-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLine.h
60 lines (50 loc) · 1.08 KB
/
Line.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
#ifndef LINE_H_INCLUDED
#define LINE_H_INCLUDED
#include<bits/stdc++.h>
#include "Main.h"
using namespace std;
void drawLine(PT a, PT b,int col,int fromWhere=0)
{
if(a.y == b.y)
{
if(a.x>b.x) swap(a,b);
for(int x = a.x; x <= b.x; x++)
{
drawPixel(PT(x, a.y), col,fromWhere);
}
}
else if(a.x == b.x)
{
if(a.y > b.y) swap(a, b);
for(int y = a.y; y <= b.y; y++)
{
drawPixel(PT(a.x, y), col,fromWhere);
}
}
else
{
double m = (double)(b.y-a.y)/(double)(b.x-a.x);
double m_inv = 1.0/m;
if(fabs(m)<=1.0)
{
if(a.x>b.x) swap(a,b);
while(a.x<=b.x)
{
drawPixel(a,col,fromWhere);
a.x+=1;
a.y+=m;
}
}
else
{
if(a.y>b.y) swap(a,b);
while(a.y<=b.y)
{
drawPixel(a,col,fromWhere);
a.x += m_inv;
a.y+=1;
}
}
}
}
#endif // LINE_H_INCLUDED