forked from tiet-ucs505/001-pixel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircle-midpoint.js
156 lines (122 loc) · 3.31 KB
/
circle-midpoint.js
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
main()
// ----------------------------------------------------
// Main
// ----------------------------------------------------
function main() {
const canvas = document.querySelector('#myCanvas')
console.log(canvas.getBoundingClientRect())
let px, py, cx, cy, r
const {ctx, imData, x0, y0, W, H}
= getContextAndPixels(canvas)
// ----------------------------------------------------
// Set a pixel
// ----------------------------------------------------
// Pixel index
px = 87
py = 27
console.log({px, py})
setPixel(px, py, imData, W, H)
flush({ctx, imData, x0, y0})
// ----------------------------------------------------
// Draw a circle
// ----------------------------------------------------
cx = 102
cy = 102
r = 50
const circlePoints = collectPointsOnCircle(cx, cy, r)
setPixels(circlePoints, imData, W, H)
flush({ctx, imData, x0, y0})
}
// ----------------------------------------------------
// Bresenham's Algorithm
// ----------------------------------------------------
function collectPointsOnOneEighthCircularArc(r) {
let x = r, y = 0, d = -r + 1.25, points = [[x, y]]
const ymax = Math.ceil(r * 0.707106781185)
while (y <= ymax) {
y += 1
x -= Number(0 < d)
points.push([x, y])
d += ((0 < d)
? (2*(y-x) + 5)
: (2*y + 3))
}
return points
}
function collectPointsOnCircle(x0, y0, r) {
let points
const copyOctants = ([x,y]) => ([
[x, y],
[x, -y],
[-x, y],
[-x, -y],
[y, x],
[y, -x],
[-y, x],
[-y, -x],
])
const spreadOut = (result, eightPoints) => (
[...result, ...eightPoints]
)
const translateOrigin = ([x, y]) => ([x+x0, y+y0])
points = collectPointsOnOneEighthCircularArc(r)
points = points.map(copyOctants)
.reduce(spreadOut)
.map(translateOrigin)
return points
}
// ----------------------------------------------------
// Canvas manipulation
// ----------------------------------------------------
function setPixels(A, imData, W, H) {
A.map(([x, y]) => setPixel(x, y, imData, W, H))
}
function getContextAndPixels(canvas) {
// ----------------------------------------------------
// Bootstrap
// ----------------------------------------------------
const ctx = canvas.getContext('2d')
const {width: W, height: H} = canvas.getBoundingClientRect()
const [x0,y0] = [0, 0]
console.log({x0, y0, W, H})
const imData = ctx.getImageData(x0,y0,W,H)
return {ctx, imData, x0, y0, W, H}
}
function setPixel(
px, py,
imData, W, H
) {
let r,g,b,a
// Set color
r = g = b = a = 255
return setPixelColor(
px, py,
r, g, b, a,
imData, W, H
)
}
function setPixelColor(
px, py,
r, g, b, a,
imData, W, H
) {
// --------------------------------------------------
// Manipulate the imData
// --------------------------------------------------
let offset, pixels
// Retrieve Image Data as pixels
pixels = imData.data
// Compute offset
offset = (py * W + px) * 4
// Set pixel value
pixels.set([r,g,b,a], offset)
// --------------------------------------------------
// Data Manipulation ends
// --------------------------------------------------
}
function flush({ctx, imData, x0, y0}) {
// --------------------------------------------------
// Flush
// --------------------------------------------------
ctx.putImageData(imData, x0, y0)
}