程序:
#include <iostream>
#include <cmath>
using namespace std;
/*
P0(x0.y0) P1(x1,y1)
*/
void DAALine(int x0,int y0,int x1,int y1)
{
int dx=x1-x0;//△x
int dy=y1-y0;//△y
int x,y;//图坐标(x,y)
double x_next,y_next;
x_next=x=x0;
y_next=y=y0;
double yInc,xInc;
int epsl;
if(abs(dx>dy))
{
epsl=abs(dx);
}
else
{
epsl=abs(dy);
}
xInc=1.0*dx/epsl;
yInc=1.0*dy/epsl;
for(int i=0;i<epsl;i++)
{
x_next=x_next+xInc;
y_next=y_next+yInc;
x=int(x_next+0.5);
y=int(y_next+0.5);
cout<<"("<<x<<","<<y<<")"<<endl;
}
}
int main()
{
int x0,y0,x1,y1;
cin>>x0>>y0>>x1>>y1;
DAALine(x0,y0,x1,y1);
return 0;
}
Matlab绘图
>> x=0:0.01:8;
>> k=5/8;
>> y=k*x;
>> plot(x,y)
>> hold on
>> X=[1 2 3 4 5 6 7 8];
>> Y=[1 1 2 3 3 4 4 5];
>> plot(X,Y,'O')
>> grid on
>>