-
Notifications
You must be signed in to change notification settings - Fork 1
/
2. Circle Drawing (Bressenham & Mid Point).cpp
104 lines (79 loc) · 2 KB
/
2. Circle Drawing (Bressenham & Mid Point).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include<bits/stdc++.h>
#include<graphics.h>
#include<windows.h>
using namespace std;
typedef pair<int,int> pii;
const int WINDOW_W = 800, WINDOW_H = 600;
pii convertPixel(int x,int y)
{
x += WINDOW_W/2;
y = -y;
y += WINDOW_H/2;
return {x,y};
}
void drawAxis()
{
for(int i=0;i<WINDOW_H;i++)
{
putpixel(400,i,WHITE);
}
for(int i=0;i<WINDOW_W;i++)
{
putpixel(i,300,WHITE);
}
}
void drawPixel(int x,int y,int col)
{
pii p = convertPixel(x,y);
putpixel(p.first,p.second,col);
}
void Bressenham(int r,int centerX,int centerY)
{
int d = 3-2*r;
int tot = round(double(r)/sqrt(2.0));
int y = round(sqrt(double(r*r-0)));
for(int i=0;i<=tot;i++)
{
drawPixel(i+centerX,y+centerY,GREEN);
drawPixel(i+centerX,-y+centerY,GREEN);
drawPixel(-i+centerX,y+centerY,GREEN);
drawPixel(-i+centerX,-y+centerY,GREEN);
drawPixel(y+centerX,i+centerY,GREEN);
drawPixel(-y+centerX,i+centerY,GREEN);
drawPixel(y+centerX,-i+centerY,GREEN);
drawPixel(-y+centerX,-i+centerY,GREEN);
if(d<0) d += 4*i+6;
else d+= 4*i-4*y+10;
if(d>0) y -= 1;
}
}
void MidPoint(int r,int centerX,int centerY)
{
double D = 5.0/4.0 - r;
int d = round(D);
int tot = round(double(r)/sqrt(2.0));
int y = round(sqrt(double(r*r-0)));
for(int i=0;i<=tot;i++)
{
drawPixel(i+centerX,y+centerY,YELLOW);
drawPixel(i+centerX,-y+centerY,YELLOW);
drawPixel(-i+centerX,y+centerY,YELLOW);
drawPixel(-i+centerX,-y+centerY,YELLOW);
drawPixel(y+centerX,i+centerY,YELLOW);
drawPixel(-y+centerX,i+centerY,YELLOW);
drawPixel(y+centerX,-i+centerY,YELLOW);
drawPixel(-y+centerX,-i+centerY,YELLOW);
if(d<=0) d += 2*i+3;
else d += 2*i-2*y+5;
if(d>0) y -= 1;
}
}
int main()
{
initwindow(WINDOW_W,WINDOW_H);
drawAxis();
Bressenham(100,200,150);
MidPoint(130,200,150);
getchar();
return 0;
}