Tuesday , December 6 2016
Home / C / C program in graphics to Draw a Line using Equation y=mx+c

C program in graphics to Draw a Line using Equation y=mx+c

#include
#include
void draw_line(float,float,float,float);
int round(float);
main()
{ int driver,mode;
 float x1,y1,x2,y2;
 clrscr();
 printf(“Enter the two end-points of the line:”);
 printf(“nx1 =”);
 scanf(“%f”,&x1);
 printf(“y1 =”);
 scanf(“%f”,&y1);
 printf(“x2 =”);
 scanf(“%f”,&x2);
 printf(“y2 =”);
 scanf(“%f”,&y2);
 clrscr();driver = DETECT;
 initgraph(&driver,&mode,”c:\\tc\\bgi”);
 draw_line(x1,y1,x2,y2);
 getch();
 closegraph();}
void draw_line(float x1,float y1,float x2,float y2)
{ float m,c;
 float x,y;
 m = (y2 – y1)/(x2 – x1);
 c = y1 – m * x1;
 for(x = x1; x<x2; x++)
    {          y = m * x + c;
                y = round(y);
                putpixel(x,(int)y,EGA_WHITE);
    }}
 
int round(float x)
{
 float temp;
 int val = (x + 0.5);
 return val;
}
 

OUTPUT:

 
Tags:Line Drawing algorithm, Draw line using its eqation, C Program for line drawing, Line drawing using y=mx+c
Also See:   Java program for patterns and formats in java/bluej/c/c++

Check Also

Hide Mouse Pointer C Program

How to Hide a Mouse Pointer Program in C : This program will show and hide ...

Leave a Reply

Your email address will not be published. Required fields are marked *

Preventing spam * Time limit is exhausted. Please reload the CAPTCHA.