A
A
Alexander Miranovich2017-11-05 11:10:09
JavaScript
Alexander Miranovich, 2017-11-05 11:10:09

Canvas draw an arbitrary figure with the mouse, how to close the figure if the lines intersect?

The task is as follows, the user can draw an arbitrary area, but if the line contour intersects, the area must be closed. I tried to track the coordinates in the array, and if the coordinates repeat, I would close the contour.
But when the mouse moves mousemove, the coordinates can immediately repeat, for this I checked for a mismatch between the previous coordinates of the line and the subsequent ones, but not all the coordinates of the line fall into the array.
But I went in the wrong direction, can someone point me in the right direction?
Thank you.
Example: https://jsfiddle.net/aleks_664/oLbbh6be/4/

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
twobomb, 2017-11-05
@aleks_664

Like this?

Checking the intersection of two lines
function intersection(ax1,ay1,ax2,ay2,bx1,by1,bx2,by2){//true есть пересечение иначе false
var v1,v2,v3,v4;
v1=(bx2-bx1)*(ay1-by1)-(by2-by1)*(ax1-bx1);
v2=(bx2-bx1)*(ay2-by1)-(by2-by1)*(ax2-bx1);
v3=(ax2-ax1)*(by1-ay1)-(ay2-ay1)*(bx1-ax1);
v4=(ax2-ax1)*(by2-ay1)-(ay2-ay1)*(bx2-ax1);
return (v1*v2<0) && (v3*v4<0);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question