码迷,mamicode.com
首页 > 其他好文 > 详细

[Matlab] 获取给定两点间直线上点集的方法

时间:2015-06-20 20:52:34      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

 

代码如下:

function [x, y] = Cooline(x1, x2, y1, y2)
%CooLINE Integer-coordinate line drawing algorithm.
%  [X, Y] = CooLINE(X1, X2, Y1, Y2) computes an
%  approximation to the line segment joining (X1, Y1) and
%  (X2, Y2) with integer coordinates.  X1, X2, Y1, and Y2

dx = abs(x2 - x1);
dy = abs(y2 - y1);
 
% Check for degenerate case.
if ((dx == 0) && (dy == 0))
   x = x1;
   y = y1;
  return;
end
 
flip = 0;
if (dx >= dy)
   if (x1 > x2)
      % Always "draw" from left to right.
      t = x1; x1 = x2; x2 = t;
      t = y1; y1 = y2; y2 = t;
      flip = 1;
   end
   m = (y2 - y1)/(x2 - x1);
   x = (x1:x2).‘;
   y = round(y1 + m*(x - x1));
else
   if (y1 > y2)
      % Always "draw" from bottom to top.
      t = x1; x1 = x2; x2 = t;
      t = y1; y1 = y2; y2 = t;
      flip = 1;
   end
   m = (x2 - x1)/(y2 - y1);
   y = (y1:y2).‘;
   x = round(x1 + m*(y - y1));
end
  
if (flip)
   x = flipud(x);
   y = flipud(y);
end
end

[Matlab] 获取给定两点间直线上点集的方法

标签:

原文地址:http://www.cnblogs.com/xiaodon1992/p/4590973.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!