The midpoint drawing line in computer graphics, the midpoint drawing circle, Bresenham drawing line and Drawing circle algorithm

Source: Internet
Author: User

#include <iostream>
#include <graphics.h>//This reference Easyx graphics library
#include <conio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
using namespace Std;

Bresenham Drawing Line
void Bresenham_line (int x0,int y0,int x1,int y1)
{
int x,y,dx,dy;
float k,e;
dx=x1-x0;
Dy=y1-y0;
k=dy/dx;//here is the initialization of the completion K value
e=-0.5;
x=x0;
Y=y0;
Here I always add a word to the end, y this is optional add one or do not add
for (int i=0;i<=dx;i++)
{
The function of drawing points
Putpixel (x,y,255);
x=x+1;
E=e+k;
if (e>=0)
{
y=y+1;
e=e-1;//if E is greater than 0, you need to lose one immediately.
}
}
}

Midpoint Draw Line
void Midpointline (int x0,int y0,int x1,int y1)
{
int a,b,delta1,delta2,d,x,y;
A=y0-y1;
b=x1-x0;
D=2*a+b;
The 2 here is to avoid the calculation of decimals
Delta1=2*a;
delta2=2* (A+B);
x=x0;
Y=y0;
Putpixel (x,y,255);
while (X<X1) {
if (d<0) {//This will select the above point
x++;y++;d +=delta2;
}else{//Select one of the following points
x + +;d +=delta1;
}
Putpixel (x,y,255);
}
}

Midpoint Draw Line

void MidpointLine2 (int x1,int y1,int x2,int y2,colorref color)//Midpoint drawing line method
{
int x = x1, y = y1; Initialize x, y
int a = Y1-y2, B = x2-x1; A, b increments of x and Y, respectively
Consider the four scenarios in which increments are calculated separately
int DELTX = (b >= 0? 1: (b =-B,-1));
int delty = (a <= 0? 1: (A = A,-1));

 for (int i=0;i<=400;i++)
 {putpixel (i, J, color);p utpixel (+, I, color);}
 putpixel (x+200, y+200, color);

 int D, DELT1, DELT2;
 if (-a <= b)   //slope absolute Value <= 1
 {
  d = 2 * a + B;
&NBSP;&NBSP;DELT1 = 2 * A;
&NBSP;&NBSP;DELT2 = 2 * (A + B);
  while (x! = x2)
  {
   if (d < 0)
    y + = Delty, D + = DELT2;
   else
    d + = DELT1;
   x + = Deltx;
   putpixel (x+200, y+200, color);
  }
 }
 else    //Slope Absolute value > 1
 {
  d = 2 * b + A;
&NBSP;&NBSP;DELT1 = 2 * b;
&NBSP;&NBSP;DELT2 = 2 * (A + B);
  while (Y! = y2)
  {
   if (d < 0)
    d + = DELT1;
   else
    x + = Deltx, D + = DELT2;
   y + = delty;
   putpixel (x+200, y+200, color);
  }
 }
}

Midpoint Draw Circle
void midpointcircle (int x0,int y0,int r,int color)

{
int x=0,y=r;
float D=5.0/4-r;
while (x<=y) {
Putpixel (X0+x,y0+y,color);
Putpixel (X0+x,y0-y,color);
Putpixel (X0-x,y0+y,color);
Putpixel (X0-x,y0-y,color);
Putpixel (X0+y,y0+x,color);
Putpixel (X0+y,y0-x,color);
Putpixel (X0-y,y0+x,color);
Putpixel (X0-y,y0-x,color);
if (d<0)
d+=x*2.0+3;
else{
d+=2.0* (x-y) +5;y--;
}
x + +;
}
}

Bresenhem Draw a circle

void bresenhemcircle (int centerx, int centery, int radius, int color)
{
int x = 0;
int y = radius;
int delta = (1-radius);
int direction;
while (y >= 0) {
Putpixel (centerx+x, centery+y, color);
Putpixel (centerx-x, centery+y, color);
Putpixel (centerx-x, centery-y, color);
Putpixel (centerx+x, centery-y, color);
if (Delta < 0) {
if ((delta+y)-1) < 0) {
Direction = 1;
}
else {
Direction = 2;
}
}
else if (Delta > 0) {
if ((delta-x)-1) <= 0) {
Direction = 2;
}
else {
Direction = 3;
}
}
else {
direction=2;
}
switch (direction) {
Case 1:
x + +;
Delta + = (2*x+1);
Break
Case 2:
x + +;
y--;
Delta + = (x-y+1);
Break
Case 3:
y--;
Delta + = ( -2*y+1);
Break
}
}
}


void Main ()
{
int x0,y0,x1,y1;

Initgraph (640, 480);
Bresenham_line (10,10,56,56);
MidpointLine2 (0,100,369,0,255);
Midpointcircle (100,100,50,255);
Bresenhemcircle (100,100,100,255);

Getch (); Press any key to continue
Closegraph (); Close the graphical interface
}

Many also do not say, the source code can be used directly, but also with the axis of

The midpoint drawing line in computer graphics, the midpoint drawing circle, Bresenham drawing line and Drawing circle algorithm

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.