展开全部
上次刚写过,在VC下运行的,
int dx,dy,incrE,incrNE,d,x,y;
if ((point[1].x-point[0].x)==0){ //垂直的直线
x=point[0].x;
for(y=point[0].y;y<point[1].y;y++)
pDC->SetPixel(x,y,50);
}
else if(abs((point[1].y-point[0].y)/(point[1].x-point[0].x))<=1){ //斜率 -1到 1 之间
dx=point[1].x-point[0].x;
dy=point[0].y-point[1].y;
d=dx-2*dy;
incrE=-2*dy;
incrNE=2*(dx-dy);
x=point[0].x,y=point[0].y;
pDC->SetPixel(x,y,50);
if(point[0].y>point[1].y){
while(x<point[1].x)
{
if(d>=0){
d+=incrE;
x++;
}
else
{d+=incrNE;
x++;
y--;
}
pDC->SetPixel(x,y,50);
}
}
else if(point[0].y<=point[1].y){
dy=point[1].y-point[0].y;
incrE=-2*dy;
incrNE=2*(dx-dy);
x=point[0].x,y=point[0].y;
pDC->SetPixel(x,y,50);
while(x<point[1].x)
{
if(d>=0){
d+=incrE;
x++;
}
else
{d+=incrNE;
x++;
y++;
}
pDC->SetPixel(x,y,50);
}
}
}
else { //斜率 <-1 和 >1的直线
if(point[1].x>=point[0].x){
dx=point[1].x-point[0].x;
dy=point[1].y-point[0].y;
d=2*dx-dy;
incrE=2*dx;
incrNE=2*(dx-dy);
x=point[0].x,y=point[0].y;
pDC->SetPixel(x,y,50);
while(x<point[1].x)
{
if(d<0){
d+=incrE;
y++;
}
else
{d+=incrNE;
pDC->SetPixel(x,y,50);
x++;
y++;
}
pDC->SetPixel(x,y,50);
}
}
else if((point[1].y-point[0].y)/(point[1].x-point[0].x)<-1){
dx=point[1].x-point[0].x;
dy=point[0].y-point[1].y;
d=2*dx-dy;
incrE=2*dx;
incrNE=2*(dx-dy);
x=point[0].x,y=point[0].y;
pDC->SetPixel(x,y,50);
while(y<point[1].y)
{
if(d>0){
d+=incrE;
y++;
}
else
{d+=incrNE;
x--;
y++;
}
pDC->SetPixel(x,y,50);
}
}
}
int dx,dy,incrE,incrNE,d,x,y;
if ((point[1].x-point[0].x)==0){ //垂直的直线
x=point[0].x;
for(y=point[0].y;y<point[1].y;y++)
pDC->SetPixel(x,y,50);
}
else if(abs((point[1].y-point[0].y)/(point[1].x-point[0].x))<=1){ //斜率 -1到 1 之间
dx=point[1].x-point[0].x;
dy=point[0].y-point[1].y;
d=dx-2*dy;
incrE=-2*dy;
incrNE=2*(dx-dy);
x=point[0].x,y=point[0].y;
pDC->SetPixel(x,y,50);
if(point[0].y>point[1].y){
while(x<point[1].x)
{
if(d>=0){
d+=incrE;
x++;
}
else
{d+=incrNE;
x++;
y--;
}
pDC->SetPixel(x,y,50);
}
}
else if(point[0].y<=point[1].y){
dy=point[1].y-point[0].y;
incrE=-2*dy;
incrNE=2*(dx-dy);
x=point[0].x,y=point[0].y;
pDC->SetPixel(x,y,50);
while(x<point[1].x)
{
if(d>=0){
d+=incrE;
x++;
}
else
{d+=incrNE;
x++;
y++;
}
pDC->SetPixel(x,y,50);
}
}
}
else { //斜率 <-1 和 >1的直线
if(point[1].x>=point[0].x){
dx=point[1].x-point[0].x;
dy=point[1].y-point[0].y;
d=2*dx-dy;
incrE=2*dx;
incrNE=2*(dx-dy);
x=point[0].x,y=point[0].y;
pDC->SetPixel(x,y,50);
while(x<point[1].x)
{
if(d<0){
d+=incrE;
y++;
}
else
{d+=incrNE;
pDC->SetPixel(x,y,50);
x++;
y++;
}
pDC->SetPixel(x,y,50);
}
}
else if((point[1].y-point[0].y)/(point[1].x-point[0].x)<-1){
dx=point[1].x-point[0].x;
dy=point[0].y-point[1].y;
d=2*dx-dy;
incrE=2*dx;
incrNE=2*(dx-dy);
x=point[0].x,y=point[0].y;
pDC->SetPixel(x,y,50);
while(y<point[1].y)
{
if(d>0){
d+=incrE;
y++;
}
else
{d+=incrNE;
x--;
y++;
}
pDC->SetPixel(x,y,50);
}
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
DFRobot
2024-11-10 广告
2024-11-10 广告
图形化编程是一种直观的编程方式,它通过拖拽图形化的编程积木来构建程序,降低了编程的学习门槛。在上海智位机器人股份有限公司,我们致力于将图形化编程应用于机器人教育等领域,使学习者能够以更加轻松、有趣的方式掌握编程技能。我们的图形化编程平台界面...
点击进入详情页
本回答由DFRobot提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询