The algorithm of Midpoint Brehensam line drawing

Source: Internet
Author: User

#include <stdio.h>

#include <stdlib.h>

#include "Graphics.h"

function declaration

void Midbrehansem (int x0, int y0, int x1, int y1); Midpoint Midbrehansem algorithm

void swap (int &x, int &y); Passing Exchange values by reference

int main ()

{

int gdriver = DETECT, Gmode;

int x0, y0, x1, y1;

printf ("Please input start point:\n");

scanf ("%d%d", &x0, &y0);

printf ("Please input end point:\n");

scanf ("%d%d", &x1, &y1);

Initgraph (&gdriver, &gmode, "");

Midbrehansem (x0, y0, x1, y1);

System ("pause");

Closegraph ();

return 0;

}

Midpoint Midbrehansem algorithm

void Midbrehansem (int x0, int y0, int x1, int y1)

{

int DX, Dy,i,di;

Double k;

if (X0 > X1)

{

Swap (x0, x1);

Swap (y0, y1);

}

int x = x0, y = y0; Defined to the front will draw lines from X1,y1

DX = x1-x0;

dy = y1-y0;

K = 1.0*DY/DX;

i = (abs (DY) >abs (dx))? ABS (DY): abs (DX);

if (K > 1)

{

DI = 2 * dx + dy;

while (i--)

{

Putpixel (x, y, RED);

if (di >= 0)

{

Di + = 2 * (dx-dy);

x + +;

}

Else

Di + = 2 * DX;

y++;

}

}

else if (k >= 0 && K < 1)

{

Di = dx-2 * DY;

while (i--)

{

Putpixel (x, y, YELLOW);

if (di < 0)

{

Di + = 2 * (dx-dy);

y++;

}

Else

Di + = ( -2) *dy;

x + +;

}

}

else if (k >=-1 && k < 0)

{

Di =-dx-2 * DY;

while (i--)

{

Putpixel (x, y, YELLOW);

if (di >= 0)

{

Di + = ( -2) * (dx + dy);

y--;

}

Else

Di + = ( -2) *dy;

x + +;

}

}

Else

{

DI =-2 * (dx + dy);

while (i--)

{

Putpixel (x, y, white);

if (di < 0)

{

Di + = ( -2) * (dx + dy);

x + +;

}

Else

Di + = ( -2) *dx;

y--;

}

}

}

Passing Exchange values by reference

void swap (int &x, int &y)

{

int t;

t = x;

x = y;

y = t;

}

The algorithm of Midpoint Brehensam line drawing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.