Untitled

 avatar
user_7016102
plain_text
a year ago
1.2 kB
6
Indexable
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>

int main()
{
    int i, gd = DETECT, gm;
    initgraph(&gd, &gm, NULL);
    int x0, y0, xn, yn;

    printf("Enter two end points :");
    scanf("%d%d%d%d", &x0, &y0, &xn, &yn);

    float dx = xn - x0;
    float dy = yn - y0;
    float m = dy / dx;

    if (fabs(dx) >= fabs(dy)) // Check if the line is more horizontal
    {
        int steps = abs(dx);

        float xinc = dx / steps;
        float yinc = m * xinc;

        float x = x0;
        float y = y0;

        for (i = 0; i <= steps; i++)
        {
            putpixel(round(x), round(y), RED);
            delay(10);
            x += xinc;
            y += yinc;
        }
    }
    else // Line is more vertical
    {
        int steps = abs(dy);

        float yinc = dy / steps;
        float xinc = 1 / m * yinc;

        float x = x0;
        float y = y0;

        for (i = 0; i <= steps; i++)
        {
            putpixel(round(x), round(y), RED);
            delay(10);
            x += xinc;
            y += yinc;
        }
    }

    getch();
    closegraph();
    return 0;
}
Editor is loading...
Leave a Comment