-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQRT_X2.py
More file actions
196 lines (136 loc) · 4.91 KB
/
Copy pathSQRT_X2.py
File metadata and controls
196 lines (136 loc) · 4.91 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import math
import cv2
import numpy as np
img = np.zeros((1080,1920,3), dtype=np.int16)
def DrawBezier(pts, nPoints):
for x in range(nPoints):
t = (1.0 * x) /(nPoints * 1.0);
k_1_minus_t = 1.0 - t
k0 = k_1_minus_t * k_1_minus_t * k_1_minus_t
k1 = 3 * k_1_minus_t * k_1_minus_t * t;
k2 = 3 * k_1_minus_t * t * t;
k3 = t * t * t;
point = k0 * pts[0] + k1 * pts[1] + k2 * pts[2] + k3 * pts[3]
#pt = point
xx = (int)(point[0]+0.500001)
yy = (int)(point[1]+0.500001)
img[yy,xx,0] = 0
img[yy,xx,1] = 0
img[yy,xx,2] = 0
def AntiAlias(pts, nPoints):
for x in range(nPoints):
t = (1.0 * x) /(nPoints * 1.0);
k_1_minus_t = 1.0 - t
k0 = k_1_minus_t * k_1_minus_t * k_1_minus_t
k1 = 3 * k_1_minus_t * k_1_minus_t * t;
k2 = 3 * k_1_minus_t * t * t;
k3 = t * t * t;
point = k0 * pts[0] + k1 * pts[1] + k2 * pts[2] + k3 * pts[3]
pt = point
xx1 = int(point[0])
yy1 = int(point[1])
if 1: #2x2 anti alias
for ii in range(2):
for jj in range(2):
xx = xx1 + ii
yy = yy1 + jj
distance = (xx - point[0]) * (xx - point[0]) + (yy - point[1]) * (yy - point[1])
distance = math.sqrt(distance)
#Blender with original Color
img[yy,xx,0] = min(255,int(255 * distance)) * img[yy,xx,0] / 255
img[yy,xx,1] = min(255,int(255 * distance)) * img[yy,xx,0] / 255
img[yy,xx,2] = min(255,int(255 * distance)) * img[yy,xx,0] / 255
#img[yy,xx,0] = min(min(255,int(255 * distance)) , img[yy,xx,0])
#img[yy,xx,1] = min(min(255,int(255 * distance)) , img[yy,xx,0])
#img[yy,xx,2] = min(min(255,int(255 * distance)) , img[yy,xx,0])
if 0:
xx = (int)(point[0]+0.500001)
yy = (int)(point[1]+0.500001)
img[yy,xx,0] = 0
img[yy,xx,1] = 0
img[yy,xx,2] = 0
def DrawLine(pt1, pt2, color):
distance = (pt1[0] - pt2[0])*(pt1[0] - pt2[0]) + (pt1[1] - pt2[1])*(pt1[1] - pt2[1])
distance = math.sqrt(distance)
# to have safe points
nPoints = int(distance * 1.2)
for x in range(nPoints):
t = (1.0 * x) /(nPoints * 1.0);
k = 1 - t
point = k * pt1 + t * pt2
xx = (int)(point[0]+0.500001)
yy = (int)(point[1]+0.500001)
img[yy,xx] = color
for y in range(1080):
for x in range(1920):
img[y,x,0]= 255
img[y,x,1]= 255
img[y,x,2]= 255
if 0:
# B(t) = (1-t)^3*P0 + 3(1-t)^2*t*P1 + 3 (1-t)*t^2 * P3 + t^3 * P3
#4 Control Points
# [200, 50], [200, 650] , [800, 650], [800,50]
# 800 * 3.14 ~= 2500 Points, 2500 Points may have leaded Points
#control_points
pts = np.zeros((4,2), dtype=np.int16)
pts[0,0] = 200
pts[0,1] = 50
pts[1,0] = 200
pts[1,1] = 850
pts[2,0] = 1600
pts[2,1] = 850
pts[3,0] = 1600
pts[3,1] = 50
DrawBezier(pts, 2500)
#Increasing Points to 3000
pts[0,1] = pts[0,1] + 200
pts[1,1] = pts[1,1] + 200
pts[2,1] = pts[2,1] + 200
pts[3,1] = pts[3,1] + 200
DrawBezier(pts, 5000)
pts[0,1] = pts[0,1] + 200
pts[1,1] = pts[1,1] + 200
pts[2,1] = pts[2,1] + 200
pts[3,1] = pts[3,1] + 200
AntiAlias(pts, 2500)
# 勾股定理, 1*1+1*1 = 2; 1*1 + sqrt(2) * sqrt(2) = 3; 1*1 + sqrt(3) * sqrt(3) = 4. ....
pts = np.zeros((3,2), dtype=np.int32)
color = np.zeros((3), dtype=np.int16)
red_color = np.zeros((3), dtype=np.int16)
pts[0,0] = 500
pts[0,1] = 600
pts[1,0] = 500
pts[1,1] = 610
color[0] = 0
color[1] = 0
color[2] = 0
red_color[0] = 0
red_color[1] = 0
red_color[2] = 255
DrawLine(pts[0], pts[1], color)
unit_1 = 10
for i in range(1000):
# compute dx, dy
dx = pts[1,0] - pts[0,0]
dy = pts[1,1] - pts[0,1]
#Vertical switch dx, dy
t = dy
dy = dx * 1.0
dx = t * 1.0
#normalize
distance = dx * dx + dy * dy
#print(i, dx, dy, distance)
distance = math.sqrt(distance)
dx = -dx / distance
dy = dy / distance
pts[2,0] = int(pts[1,0] + unit_1 * dx)
pts[2,1] = int(pts[1,1] + unit_1 * dy)
#print(pts)
#Draw Vertical Edge
DrawLine(pts[1], pts[2], color)
#bevel edge
DrawLine(pts[0], pts[2], red_color)
#Change Pts2 to pts1;
pts[1] = pts[2]
cv2.imwrite('sqrt_xx.jpg', img)
cv2.imshow('image',img)