利用Bresenham算法,从零开始实现在矩阵中画圆和直线,效果如下:
基于matlab的画图代码如下:
close all; clear all; clc
% create image reactangle:
img = ones(100, 200);
figure('Name', 'original image')
imshow(img)
% draw line:
help bresenham_line
draw_value = 0;
x1 = 80; y1 = 20;
x2 = 20; y2 = 60;
img = bresenham_line(img, x1, y1, x2, y2, draw_value);
% draw circle:
help bresenham_circle
cx = 50; cy = 50;
r = 30;
img = bresenham_circle(img, cx, cy, r, draw_value);
figure('Name', 'after drawn')
imshow(img)
在上面代码中,我分别写了画直线和画圆的代码,其中画直线函数 “bresenham_line.m” 内容如下:
function [img] = bresenham_line(img, x1, y1, x2, y2, value)
%bresenham_line(img

本文介绍了如何使用Bresenham算法在矩阵中从零开始实现画圆和直线。提供了MATLAB代码示例,包括`bresenham_line.m`用于画直线,`bresenham_circle`用于画圆。读者可以通过在MATLAB命令行窗口输入相关函数名获取使用帮助。
最低0.47元/天 解锁文章
4533

被折叠的 条评论
为什么被折叠?



