1

このコードを作りました。このコードは、dx と dy が 0 より大きい場合を処理します。しかし、どちらかが 0 より小さい場合はどうすればよいでしょうか。アルゴリズムは、m を絶対値として取り、x と y を減分する必要があると述べています。しかし、2 番目の for ループで i(x) を減らす方法は?

#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <graphics.h>

void illuminate (int x, int y,  int col)
{
    putpixel(x+320,240-y,col);
}


void main (void)
{
    int i,x1,y1,x2,y2,j;
    float dx,dy,m,e;
    int gdriver=DETECT,gmode;

    initgraph(&gdriver,&gmode, "C:\\TurboC3\\BGI");
    printf("\nEnter the coordinates of initial point\n");
    scanf("%d%d",&x1,&y1);
    printf("\nEnter the coordinates of final point\n");
    scanf("%d%d",&x2,&y2);

    dx = x2 - x1;
    dy = y2 - y1;
    m = abs(dy)/abs(dx);
    j = y1;
    e = m - 1;

    line(320,0,320,480);
    line(0,240,640,240);

    if ( dx >= 0 && dy >= 0 )   
    {
        for (i=x1;i<x2-1;i++)
        {
            illuminate(i,j,4);
            if ( e>= 0 )
            {
                j = j+1;
                e = e-1.0;
            }
            e = e+m;
        }
    }
    else
    {
        for (i=x1;i<x2-1;i++)
        {
            illuminate(i,j,4);
            if (e>=0)
            {
                j = j-1;
                e = e-1.0;
            }
            e = e+m;
        }
    }
    getch();
}
4

1 に答える 1