Experiencing strange recursion behavior with setTimeout - javascript

I wrote a simple 3d flood-filling algorithm to draw a diamond shape into an array (that array is pseudo multi dimensional as described in this post). The actual method looks like this:
/**
* Draws a 3d diamond shape into an array using a flood fill algorithm.
*
* #param data The array to "draw" the diamond shape in. Must be of length 100 * 100 * 100 and all values must be 0.
* #param pos Tupel of x, y, z position to be the center of the diamond.
*/
export function flood3dDiamondShape(data: Uint8Array, pos: [number, number, number], distance = 0): void {
if (distance > 10) {
return;
}
const [x, y, z] = pos;
const index = (x) + (z * 100) + (y * 100 * 100);
if (data[index] > 0) {
return;
}
data[index] = 1;
flood3dDiamondShape(data, [x + 1, y, z], distance + 1);
flood3dDiamondShape(data, [x - 1, y, z], distance + 1);
flood3dDiamondShape(data, [x, y + 1, z], distance + 1);
flood3dDiamondShape(data, [x, y - 1, z], distance + 1);
flood3dDiamondShape(data, [x, y, z + 1], distance + 1);
flood3dDiamondShape(data, [x, y, z - 1], distance + 1);
}
However, this doesn't result in the expected result but draws a strangely shaped "blob" into the array (refer to image 2). I tried to debug that behavior by wrapping the six calls to flood3dDiamondShape(...) into a setTimeout callback. It looks like this:
setTimeout(() => {
flood3dDiamondShape(data, [x + 1, y, z], distance + 1);
flood3dDiamondShape(data, [x - 1, y, z], distance + 1);
flood3dDiamondShape(data, [x, y + 1, z], distance + 1);
flood3dDiamondShape(data, [x, y - 1, z], distance + 1);
flood3dDiamondShape(data, [x, y, z + 1], distance + 1);
flood3dDiamondShape(data, [x, y, z - 1], distance + 1);
}, 1);
This still works but now the strange "blob" isn't there anymore. It now is the expected diamond shape as you may see in image 3.
Note that using the second method I of course waited for the algorithm to finish before actually drawing the shape.
The question now actually is: Why am I experiencing this behavior? I would like to get the diamond shape 3 but without using setTimeout.
Minimum working example: I created a 2d version in html to show the algorithm. Please refer to: https://pastebin.com/raw/MvyJzR5x. How it looks: 4
Minimum working example in 2d
How it is supposed to look (with setTimeout)
How it actually looks (without setTimeout)

I somewhat solved it. The problem was with the distance field. The algorithm will walk through the array like a snake and will eventually come to a stop because the distance got too big. As the already visited fields will now check for painted fields to the top/bottom, left/right their may already be paint by the first "snake" and no more is painted. This results in an unwanted shape.
Now this isn't really a solution but it explains the problem. I thought about replacing the base case distance > 10 with something like manhattenDistance(x, y, originalX, originalY). I am not able to come up with another solution.
By the way: The setTimeout solved the issue because the order in which the pixels are worked off is essentially reversed. This prevents the algorithm from becoming stuck. This could also be implemented with a list that keeps an order of when to work off which pixel.
EDIT:
I now found a way to keep the original distance field. I initially introduced it to ensure that the shape "bleeds" around already existing shapes in the data array. Using the manhattenDistance the shape would ignore boundaries. The result now looks like this:. The updated algorithm (in 2d like the provided example) now looks like this:
function floodFill(data, x, y, distance) {
if (distance <= 0) {
return;
}
const index = (x) + (y * 100);
if (data[index] >= distance) {
return;
}
data[index] = distance;
floodFill(data, x, y + 1, distance - 1);
floodFill(data, x, y - 1, distance - 1);
floodFill(data, x + 1, y, distance - 1);
floodFill(data, x - 1, y, distance - 1);
}
As you may see it was just a matter of flipping the base case of comparing the already painted color to the color that is to be painted.

Related

Loop construction in Processing artistic rendition

The code is from here:
t=0
draw=_=>{t||createCanvas(W = 720,W)
t+=.01
B=blendMode
colorMode(HSB)
B(BLEND)
background(0,.1)
B(ADD)
for(y = 0; y < W; y += 7)
for(x = 0; x < W; x+=7)
dist(x, y, H = 360, H) +
!fill(x * y % 360, W, W,
T=tan(noise(x, y) * 9 + t))
< sin(atan2(y - H, x - H) * 2.5 + 84)**8 * 200 + 130?
circle(x, y + 30, 4/T) : 0}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.5.0/p5.js"></script>
I see that t is increased by 0.01 each iteration, but I am unsure whether for each t value the entire canvas is refreshed, or whether there is an endpoint somewhat set by :0}. Is this correct?
It also seems like there is a Boolean call in the middle basically comparing two distances to determine which circles are filled and how. If the < was instead > the circles would form a complementary pattern on the rendition.
The ! is explained here, as a way of saving space and calling a function as soon as it is declared. I presume it determines how points are filled with a certain variable color scheme depending on the Boolean operation result. The +!fill() is unfamiliar, as well as the ? at the end, and I guess that they amount to: if the x, y location is within the boundary of the star the circles are colored (filled) as such, but the negation in '!' is confusing.
Can I get an English explanation of the main structural points on this code (the loop and the Boolean) to match the syntax?
I have so far gathered that the basic loop is
for(y from 0 to the width of the canvas at increments of 7)
for(x from... )
check if the distance from (x , y) to 360 is less than sin()^8 * 200 + 130
If so fill (or not fill with the ! ????) with these colors
otherwise do nothing :0
This is what it might look like if it were written normally
let t = 0;
const W = 720;
// https://p5js.org/reference/#/p5/draw
// `draw` needs to be in the global scope so p5 can use it
draw = () => {
// create a canvas if this is the first frame
if (!t) createCanvas(W, W);
t += 0.01;
// Use HSB and blending to do the fancy effects
// The black circles are because we're ignoring stroke and so using its defaults
// The blending will eventually hide it anyway
colorMode(HSB);
blendMode(BLEND);
background(0, 0.1);
blendMode(ADD);
// iterate over 7px grid
for(y = 0; y < W; y += 7) {
for(x = 0; x < W; x += 7) {
// center
const H = 360;
// distance from center
const D = dist(x, y, H, H);
// pick an alpha based on location and time
const T = tan(noise(x, y) * 9 + t);
// set fill color
fill(x * y % 360, W, W, T);
// magic to calculate the star's boundary
// sine wave in polar coords, I think
const S = sin(atan2(y - H, x - H) * 2.5 + 84)**8 * 200 + 130;
// draw a circle if we're within the star's area
// circle's color, alpha, and radius depend on location and time
if (D < S) circle(x, y + 30, 4/T);
}
}
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.5.0/p5.js"></script>
Note that there are some hacks in the original code that are solely to help make it a one-liner or to reduce the number of characters. They don't have any meaningful effect on the results.
The main issue is the +! with ? and :0, which is terribly confusing, but clearly it is equivalent to
t=0
draw=_=>{t||createCanvas(W = 720,W)
t+=.01
B=blendMode
colorMode(HSB)
B(BLEND)
background(0,.1)
B(ADD)
for(y = 0; y < W; y += 7)
for(x = 0; x < W; x+=7)
if(dist(x, y, H = 360, H) < sin(atan2(y - H, x - H) * 2.5 + 84)**8 * 200 + 130){
fill(x * y % 360, W, W,
T=tan(noise(x, y) * 9 + t))
circle(x, y + 30, 4/T)}else{0}}
The Boolean in the ? applies to the dist() part (in absolute values the angle has to be less than sin()... + 130.
The + part I still don't understand, and hopefully will be addressed by someone who knows Processing or JS (not me). However, it probably forces the execution to identify and throw out with regards to filling (hence !) values that are too low for the sin(atan2()), which will happen at around 0 and pi.
Because of arctan2() being (here)
the number of spikes in the star will be a multiple of 5 going from 0 to 2 pi. The fact that changing the code to < sin(2 * atan2(...)... doubles the spikes implies that the fill() part is also in Boolean operation of its own.
The result of the Boolean determines whether the colorful fill is applied or not (applied if less than).
The :0 at the end is the else do nothing.

How to project a 2d point onto a 2d axis in javascript

I have a point defined like {x:(x value), y: (y value)}, and I have an axis with a slope and a y intercept. I am trying to project the point onto the line. I have searched 'project point onto line' and looked around for a long time, but i can't find anything that projects a point onto a slope and intercept line.
This code works for me (a slight change from OP's answer):
function project (x, y, slope, yint) {
var slope2 = -1 / slope;
var yint2 = y - slope2 * x;
var nx = (yint2 - yint) / (slope - slope2);
return {x: nx, y: (slope2 * nx) + yint};
}
The actual answer was simple. I just needed to make a perpendicular line to the other line that touches the point being projected. Then, the projection was just the point of intersection of both of the lines. So I implemented a function in javascript, the parameters being the point's x, the point's y, the line's slope, and the line's y intercept, and it returns the projection as {x, y}.
function project (x, y, slope, yint) {
var slope2 = -1 / slope;
var yint2 = y - slope2 * x;
var nx = (yint2 - yint) / (slope - slope2);
return {x: nx, y: (slope2 * nx) + yint}; //thanks to #igobivo for fixing that mistake
}

hough transform - javascript - node.js

So, i'm trying to implement hough transform, this version is 1-dimensional (its for all dims reduced to 1 dim optimization) version based on the minor properties.
Enclosed is my code, with a sample image... input and output.
Obvious question is what am i doing wrong. I've tripled check my logic and code and it looks good also my parameters. But obviously i'm missing on something.
Notice that the red pixels are supposed to be ellipses centers , while the blue pixels are edges to be removed (belong to the ellipse that conform to the mathematical equations).
also, i'm not interested in openCV / matlab / ocatve / etc.. usage (nothing against them).
Thank you very much!
var fs = require("fs"),
Canvas = require("canvas"),
Image = Canvas.Image;
var LEAST_REQUIRED_DISTANCE = 40, // LEAST required distance between 2 points , lets say smallest ellipse minor
LEAST_REQUIRED_ELLIPSES = 6, // number of found ellipse
arr_accum = [],
arr_edges = [],
edges_canvas,
xy,
x1y1,
x2y2,
x0,
y0,
a,
alpha,
d,
b,
max_votes,
cos_tau,
sin_tau_sqr,
f,
new_x0,
new_y0,
any_minor_dist,
max_minor,
i,
found_minor_in_accum,
arr_edges_len,
hough_file = 'sample_me2.jpg',
edges_canvas = drawImgToCanvasSync(hough_file); // make sure everything is black and white!
arr_edges = getEdgesArr(edges_canvas);
arr_edges_len = arr_edges.length;
var hough_canvas_img_data = edges_canvas.getContext('2d').getImageData(0, 0, edges_canvas.width,edges_canvas.height);
for(x1y1 = 0; x1y1 < arr_edges_len ; x1y1++){
if (arr_edges[x1y1].x === -1) { continue; }
for(x2y2 = 0 ; x2y2 < arr_edges_len; x2y2++){
if ((arr_edges[x2y2].x === -1) ||
(arr_edges[x2y2].x === arr_edges[x1y1].x && arr_edges[x2y2].y === arr_edges[x1y1].y)) { continue; }
if (distance(arr_edges[x1y1],arr_edges[x2y2]) > LEAST_REQUIRED_DISTANCE){
x0 = (arr_edges[x1y1].x + arr_edges[x2y2].x) / 2;
y0 = (arr_edges[x1y1].y + arr_edges[x2y2].y) / 2;
a = Math.sqrt((arr_edges[x1y1].x - arr_edges[x2y2].x) * (arr_edges[x1y1].x - arr_edges[x2y2].x) + (arr_edges[x1y1].y - arr_edges[x2y2].y) * (arr_edges[x1y1].y - arr_edges[x2y2].y)) / 2;
alpha = Math.atan((arr_edges[x2y2].y - arr_edges[x1y1].y) / (arr_edges[x2y2].x - arr_edges[x1y1].x));
for(xy = 0 ; xy < arr_edges_len; xy++){
if ((arr_edges[xy].x === -1) ||
(arr_edges[xy].x === arr_edges[x2y2].x && arr_edges[xy].y === arr_edges[x2y2].y) ||
(arr_edges[xy].x === arr_edges[x1y1].x && arr_edges[xy].y === arr_edges[x1y1].y)) { continue; }
d = distance({x: x0, y: y0},arr_edges[xy]);
if (d > LEAST_REQUIRED_DISTANCE){
f = distance(arr_edges[xy],arr_edges[x2y2]); // focus
cos_tau = (a * a + d * d - f * f) / (2 * a * d);
sin_tau_sqr = (1 - cos_tau * cos_tau);//Math.sqrt(1 - cos_tau * cos_tau); // getting sin out of cos
b = (a * a * d * d * sin_tau_sqr ) / (a * a - d * d * cos_tau * cos_tau);
b = Math.sqrt(b);
b = parseInt(b.toFixed(0));
d = parseInt(d.toFixed(0));
if (b > 0){
found_minor_in_accum = arr_accum.hasOwnProperty(b);
if (!found_minor_in_accum){
arr_accum[b] = {f: f, cos_tau: cos_tau, sin_tau_sqr: sin_tau_sqr, b: b, d: d, xy: xy, xy_point: JSON.stringify(arr_edges[xy]), x0: x0, y0: y0, accum: 0};
}
else{
arr_accum[b].accum++;
}
}// b
}// if2 - LEAST_REQUIRED_DISTANCE
}// for xy
max_votes = getMaxMinor(arr_accum);
// ONE ellipse has been detected
if (max_votes != null &&
(max_votes.max_votes > LEAST_REQUIRED_ELLIPSES)){
// output ellipse details
new_x0 = parseInt(arr_accum[max_votes.index].x0.toFixed(0)),
new_y0 = parseInt(arr_accum[max_votes.index].y0.toFixed(0));
setPixel(hough_canvas_img_data,new_x0,new_y0,255,0,0,255); // Red centers
// remove the pixels on the detected ellipse from edge pixel array
for (i=0; i < arr_edges.length; i++){
any_minor_dist = distance({x:new_x0, y: new_y0}, arr_edges[i]);
any_minor_dist = parseInt(any_minor_dist.toFixed(0));
max_minor = b;//Math.max(b,arr_accum[max_votes.index].d); // between the max and the min
// coloring in blue the edges we don't need
if (any_minor_dist <= max_minor){
setPixel(hough_canvas_img_data,arr_edges[i].x,arr_edges[i].y,0,0,255,255);
arr_edges[i] = {x: -1, y: -1};
}// if
}// for
}// if - LEAST_REQUIRED_ELLIPSES
// clear accumulated array
arr_accum = [];
}// if1 - LEAST_REQUIRED_DISTANCE
}// for x2y2
}// for xy
edges_canvas.getContext('2d').putImageData(hough_canvas_img_data, 0, 0);
writeCanvasToFile(edges_canvas, __dirname + '/hough.jpg', function() {
});
function getMaxMinor(accum_in){
var max_votes = -1,
max_votes_idx,
i,
accum_len = accum_in.length;
for(i in accum_in){
if (accum_in[i].accum > max_votes){
max_votes = accum_in[i].accum;
max_votes_idx = i;
} // if
}
if (max_votes > 0){
return {max_votes: max_votes, index: max_votes_idx};
}
return null;
}
function distance(point_a,point_b){
return Math.sqrt((point_a.x - point_b.x) * (point_a.x - point_b.x) + (point_a.y - point_b.y) * (point_a.y - point_b.y));
}
function getEdgesArr(canvas_in){
var x,
y,
width = canvas_in.width,
height = canvas_in.height,
pixel,
edges = [],
ctx = canvas_in.getContext('2d'),
img_data = ctx.getImageData(0, 0, width, height);
for(x = 0; x < width; x++){
for(y = 0; y < height; y++){
pixel = getPixel(img_data, x,y);
if (pixel.r !== 0 &&
pixel.g !== 0 &&
pixel.b !== 0 ){
edges.push({x: x, y: y});
}
} // for
}// for
return edges
} // getEdgesArr
function drawImgToCanvasSync(file) {
var data = fs.readFileSync(file)
var canvas = dataToCanvas(data);
return canvas;
}
function dataToCanvas(imagedata) {
img = new Canvas.Image();
img.src = new Buffer(imagedata, 'binary');
var canvas = new Canvas(img.width, img.height);
var ctx = canvas.getContext('2d');
ctx.patternQuality = "best";
ctx.drawImage(img, 0, 0, img.width, img.height,
0, 0, img.width, img.height);
return canvas;
}
function writeCanvasToFile(canvas, file, callback) {
var out = fs.createWriteStream(file)
var stream = canvas.createPNGStream();
stream.on('data', function(chunk) {
out.write(chunk);
});
stream.on('end', function() {
callback();
});
}
function setPixel(imageData, x, y, r, g, b, a) {
index = (x + y * imageData.width) * 4;
imageData.data[index+0] = r;
imageData.data[index+1] = g;
imageData.data[index+2] = b;
imageData.data[index+3] = a;
}
function getPixel(imageData, x, y) {
index = (x + y * imageData.width) * 4;
return {
r: imageData.data[index+0],
g: imageData.data[index+1],
b: imageData.data[index+2],
a: imageData.data[index+3]
}
}
It seems you try to implement the algorithm of Yonghong Xie; Qiang Ji (2002). A new efficient ellipse detection method 2. p. 957.
Ellipse removal suffers from several bugs
In your code, you perform the removal of found ellipse (step 12 of the original paper's algorithm) by resetting coordinates to {-1, -1}.
You need to add:
`if (arr_edges[x1y1].x === -1) break;`
at the end of the x2y2 block. Otherwise, the loop will consider -1, -1 as a white point.
More importantly, your algorithm consists in erasing every point which distance to the center is smaller than b. b supposedly is the minor axis half-length (per the original algorithm). But in your code, variable b actually is the latest (and not most frequent) half-length, and you erase points with a distance lower than b (instead of greater, since it's the minor axis). In other words, you clear all points inside a circle with a distance lower than latest computed axis.
Your sample image can actually be processed with a clearing of all points inside a circle with a distance lower than selected major axis with:
max_minor = arr_accum[max_votes.index].d;
Indeed, you don't have overlapping ellipses and they are spread enough. Please consider a better algorithm for overlapping or closer ellipses.
The algorithm mixes major and minor axes
Step 6 of the paper reads:
For each third pixel (x, y), if the distance between (x, y) and (x0,
y0) is greater than the required least distance for a pair of pixels
to be considered then carry out the following steps from (7) to (9).
This clearly is an approximation. If you do so, you will end up considering points further than the minor axis half length, and eventually on the major axis (with axes swapped). You should make sure the distance between the considered point and the tested ellipse center is smaller than currently considered major axis half-length (condition should be d <= a). This will help with the ellipse erasing part of the algorithm.
Also, if you also compare with the least distance for a pair of pixels, as per the original paper, 40 is too large for the smaller ellipse in your picture. The comment in your code is wrong, it should be at maximum half the smallest ellipse minor axis half-length.
LEAST_REQUIRED_ELLIPSES is too small
This parameter is also misnamed. It is the minimum number of votes an ellipse should get to be considered valid. Each vote corresponds to a pixel. So a value of 6 means that only 6+2 pixels make an ellipse. Since pixels coordinates are integers and you have more than 1 ellipse in your picture, the algorithm might detect ellipses that are not, and eventually clear edges (especially when combined with the buggy ellipse erasing algorithm). Based on tests, a value of 100 will find four of the five ellipses of your picture, while 80 will find them all. Smaller values will not find the proper centers of the ellipses.
Sample image is not black & white
Despite the comment, sample image is not exactly black and white. You should convert it or apply some threshold (e.g. RGB values greater than 10 instead of simply different form 0).
Diff of minimum changes to make it work is available here:
https://gist.github.com/pguyot/26149fec29ffa47f0cfb/revisions
Finally, please note that parseInt(x.toFixed(0)) could be rewritten Math.floor(x), and you probably want to not truncate all floats like this, but rather round them, and proceed where needed: the algorithm to erase the ellipse from the picture would benefit from non truncated values for the center coordinates. This code definitely could be improved further, for example it currently computes the distance between points x1y1 and x2y2 twice.

Highlight coordinates on Canvas Oval boundaries

I am drawing oval using bezierCurveTo method of canvas. I have to highlight the points on boundary of oval(same as oval shape in powerpoint). I want the exact position of all eight coordinates to place them on canvas. Please refer to attached screenshot
I would recommend not to use a Bezier to create an oval/ellipse - it's mathematical inaccurate and will just cause headache when you want to use points such as in this case.
I would suggest to create your own ellipse function - it's easy; this creates an ellipse as a path which you can fill and stroke etc.:
function drawEllipse(cxt, cx, cy, rx, ry) {
ctx.beginPath();
ctx.moveTo(cx + rx, cy);
for(var a = 0, step = 0.02, max = Math.PI * 2; a < max; a += step)
ctx.lineTo(cx + rx * Math.cos(a), cy+ ry * Math.sin(a));
}
Now, to get those edge points all you need to do is have a similar function (or modify the previous) doing the same but with less granular steps as well as returning the calculated points - count is number of points you want. The resulting array here in this example will return points arranges as [x1, y1, x2, y2, ...] - this is something you can adjust as you need:
function getEllipsePoints(cxt, cx, cy, rx, ry, count) {
var points = [],
a = 0, max = Math.PI * 2,
step = max / count
for(; a < max; a += step)
points.push(cx + rx * Math.cos(a), cy+ ry * Math.sin(a));
return points;
}
Now you can plot the points at the edges as you want (and have a mathematical correct ellipse as well as hit points for mouse).
Live demo here
Result:

How can we find intersection of circle and polygon for no point of polygon in circle

Suppose i have one polygon and one circle of google drawingmanager (google maps). The scenario is that no point of polygon is in the circle but still intersecting. How can i check the intersection in this case? ![Circle and Polygon are intersecting and there is no point of polygon inside the circle][1]
Check each point of polygon for this condition:
dx^2+dy^2 < r^2
where dx = px[i] - cx, dy = py[i] - cy,
px[i], py[i] - i-point of polygon, cx,cy - circle center, r - radius
If its true for at least one i, then intersection have place.
Update:
In case if no point is directly in circle, it's getting harder. You'll need to check each line against circle for intersections.
To detect intersection of line with circle, use following check:
line equation is l(t) = [lx(t), ly(t)]
where lx = x0 + t*(x1-x0), ly = y0 + t*(y1-y0), and t here is variable between 0 and 1
if line intersects circle, there will be such value of t (say, t0), with which
l0x = lx(t0), l0y = ly(t0) fills condition
(l0x - cx)^2 + (l0y - cy)^2 < r^2
we need to find t0, and check if it's in range 0..1, here how we do it:
(x0 + t0*(x1-x0) - cx)^2 + (y0 + t0*(y1-y0) - cy)^2 = r^2
by solving this quadratic equation we will find 0, 1 or 2 solutions of t0
if it's 0 solutions - circle never intersects line
if it's 1 solution - circle touches line in 1 point
if it's 2 solution (t0a, t0b) - circle intersects line in 2 points, and additional check will be needed to check if range [t0a, t0b] intersects with range [0, 1]
to solve this equation we need normalize it:
(x0 + t0*(x1-x0) - cx)^2 + (y0 + t0*(y1-y0) - cy)^2 = r^2 equal to
((x0-cx) + t0*(x1-x0))^2 + ((y0 - cy) + t0*(y1-y0))^2 - r^2 = 0 equal to
(x0-cx)^2 + (t0*(x1-x0))^2 + 2*t0*(x1-x0)*(x0-cx) + (y0-cy)^2 + (t0*(y1-y0))^2 + 2*t0*(y1-y0)*(y0-cy) - r^2 = 0 equal to
t0^2 * A + t0 * B + C = 0, where
A = (x1-x0)^2 + (y1-y0)^2
B = 2*(x1-x0)*(x0-cx) + 2*(y1-y0)*(y0-cy)
C = (x0-cx)^2 + (y0-cy)^2 - r^2
I will not write here how to solve such standart quadratic equation, it's offtopic.
If you will have a solution with 2 values, say t0a and t0b, then you need to check it against range [0, 1].
For example:
t0a = -3.4, t1a = 2.1 - intersection occurs,
t0a = -3.4, t1a = -2.1 - intersection not occurs,
t0a = 0, t1a = 0.5 - intersection occurs
Yeah, and probably you will need to sort t0a and t0b, there is no guaranty that t0a < t0b
You will need to run this check for each line of polygon.

Categories

Resources