-
Notifications
You must be signed in to change notification settings - Fork 55
/
draw.h
109 lines (100 loc) · 2.15 KB
/
draw.h
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
105
106
107
108
109
#include <iostream>
#include <windows.h>
constexpr int WIDTH = 1000,HEIGHT = 600;
constexpr int dW=8,dH=16;
void gotoxy ( short x, short y )
{
COORD coord = {x, y};
SetConsoleCursorPosition ( GetStdHandle ( STD_OUTPUT_HANDLE ), coord );
}
void plotLineLow(char platno[HEIGHT/dH][WIDTH/dW+1],int x0,int y0,int x1,int y1,char c);
void plotLineHigh(char platno[HEIGHT/dH][WIDTH/dW+1],int x0,int y0,int x1,int y1,char c);
void drawPoint(char platno[HEIGHT/dH][WIDTH/dW+1],int A,int B,char c);
void drawLine(char platno[HEIGHT/dH][WIDTH/dW+1],int A,int B,int C,int D,char c);
void drawPoint(char platno[HEIGHT/dH][WIDTH/dW+1],int A,int B,char c)
{
if(A<0||B<0||A>=WIDTH/dW||B>=HEIGHT/dH) return;
platno[B][A]=c;
}
void drawLine(char platno[HEIGHT/dH][WIDTH/dW+1],int A,int B,int C,int D,char c)
{
//sorting
if(A>C)
{
int t;
t=A;
A=C;
C=t;
t=B;
B=D;
D=t;
}
//algorithm
if(B==D)
{
for(int i=A;i<=C;i++)
drawPoint(platno,i,B,c);//platno[B][i]=c;
return;
}
if(A==C)
{
int min=B,max=D;
if(D<B)
{
min=D;
max=B;
}
for(int i=min;i<=max;i++)
drawPoint(platno,A,i,c);//platno[i][A]=c;
return;
}
if(abs(D-B)<abs(C-A))
plotLineLow(platno,A,B,C,D,c);
else
{
if(B>D) plotLineHigh(platno,C,D,A,B,c);
else plotLineHigh(platno,A,B,C,D,c);
}
}
void plotLineLow(char platno[HEIGHT/dH][WIDTH/dW+1],int x0,int y0,int x1,int y1,char c)
{
int dx = x1 - x0, dy = y1 - y0, yi = 1;
if (dy < 0)
{
yi = -1;
dy = -dy;
}
int D = 2*dy - dx;
int y = y0;
for (int x=x0;x<=x1;x++)
{
drawPoint(platno,x,y,c);
if( D > 0)
{
y += yi;
D -= 2*dx;
}
D += 2*dy;
}
}
void plotLineHigh(char platno[HEIGHT/dH][WIDTH/dW+1],int x0,int y0,int x1,int y1,char c)
{
int dx = x1 - x0, dy = y1 - y0, xi = 1;
if (dx < 0)
{
xi = -1;
dx = -dx;
}
int D = 2*dx - dy;
int x = x0;
for (int y=y0;y<=y1;y++)
{
drawPoint(platno,x,y,c);
if( D > 0)
{
x += xi;
D -= 2*dy;
}
D += 2*dx;
}
}