Skip to content

Commit b4965e7

Browse files
committed
Use pointInPolygon for edge detection, remove edge detection option now that it works correctly
1 parent 7561db6 commit b4965e7

3 files changed

Lines changed: 30 additions & 23 deletions

File tree

ishihara/ishihara.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ const ishihara_input = {
3636
text: '',
3737
circular: true,
3838
resize: true,
39-
edge_detection: true,
4039
color_scheme: 'General 1',
4140
min_radius: (canvas.width + canvas.height) / 800,
4241
max_radius: (canvas.width + canvas.height) / 100,
@@ -188,11 +187,9 @@ gui.add(ishihara_input, 'color_scheme', ['General 1', 'General 2', 'General 3',
188187
gui.add(ishihara_input, 'text').name('Text').onChange(() => renderText());
189188
gui.add(ishihara_input, 'circular').name('Circular');
190189
gui.add(ishihara_input, 'resize').name('Resize');
191-
gui.add(ishihara_input, 'edge_detection').name('Edge detection');
192190
gui.add(ishihara_input, 'shape_factory', ['Circle', 'Regular polygon', 'Cross', 'Star', 'Heart']).onChange(value => {
193191
hide_gui_element(gui, 'sides', value !== 'Regular polygon' && value !== 'Star');
194192
hide_gui_element(gui, 'pointiness', value !== 'Cross' && value !== 'Star');
195-
ishihara_input.edge_detection = value === 'Circle';
196193
update_gui(gui);
197194
}).name('Shape');
198195
gui.add(ishihara_input, 'sides', 3, 12, 1).name('Sides');

ishihara/shape_factories.js

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -160,25 +160,40 @@ export class RegularPolygonFactory {
160160
}
161161

162162
overlaps_image(img_data, polygon) {
163-
const points = [{ x: polygon.x, y: polygon.y }];
164-
for (const p of polygon.points) {
165-
points.push({ x: polygon.x + p.x, y: polygon.y + p.y });
163+
const pts = polygon.points.map(p => ({ x: polygon.x + p.x, y: polygon.y + p.y }));
164+
165+
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
166+
for (const p of pts) {
167+
if (p.x < minX) minX = p.x;
168+
if (p.y < minY) minY = p.y;
169+
if (p.x > maxX) maxX = p.x;
170+
if (p.y > maxY) maxY = p.y;
166171
}
167-
let points_overlapping = 0;
168172

169-
for (const { x, y } of points) {
170-
const index = (Math.floor(y) * img_data.width + Math.floor(x)) * 4;
171-
const r = img_data.data[index];
172-
const g = img_data.data[index + 1];
173-
const b = img_data.data[index + 2];
174-
const a = img_data.data[index + 3];
173+
minX = Math.max(0, Math.floor(minX));
174+
minY = Math.max(0, Math.floor(minY));
175+
maxX = Math.min(img_data.width - 1, Math.ceil(maxX));
176+
maxY = Math.min(img_data.height - 1, Math.ceil(maxY));
177+
178+
let total_points = 0;
179+
let points_overlapping = 0;
175180

176-
if ((r + g + b) * (a / 255) < 127) {
177-
points_overlapping++;
181+
for (let py = minY; py <= maxY; py++) {
182+
for (let px = minX; px <= maxX; px++) {
183+
if (!pointInPolygon(px, py, pts)) continue;
184+
total_points++;
185+
const index = (py * img_data.width + px) * 4;
186+
const r = img_data.data[index];
187+
const g = img_data.data[index + 1];
188+
const b = img_data.data[index + 2];
189+
const a = img_data.data[index + 3];
190+
if ((r + g + b) * (a / 255) < 127) {
191+
points_overlapping++;
192+
}
178193
}
179194
}
180195

181-
return [points.length, points_overlapping];
196+
return [total_points, points_overlapping];
182197
}
183198

184199
intersects(polygon1, polygon2) {

ishihara/worker.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,8 @@ self.onmessage = function(e) {
130130
const [total_points, points_overlapping] = shape_factory.overlaps_image(options.img_data, shape);
131131

132132
overlaps_image = points_overlapping !== 0;
133-
134-
if (options.edge_detection) {
135-
if (overlaps_image && points_overlapping / total_points < 0.9) {
136-
continue outer;
137-
}
138-
} else if (overlaps_image) {
139-
break;
133+
if (overlaps_image && points_overlapping / total_points < 0.9) {
134+
continue outer;
140135
}
141136
}
142137

0 commit comments

Comments
 (0)