Joao Correia
Joao Correia

Reputation: 197

How to get edges of a polygon using pixijs?

I'm using pixi.js to create some editable polygons. So, what I want to achieve is this:

  1. I have one polygon

  1. Then, when I hit the edge a small circle should appear

enter image description here

  1. And next I can drag and drop that part of the edge to creating a new point for the polygon

enter image description here

For now, what I know is the polygon vertices and I'm thinking to use the line function (y=mx+b) to check if the point where the mouse is belongs to the edge. My problem here, is that I have no idea how to obtain that edges. Any Suggestion? Of course, if you have any other idea to do this feel free to share =).

Upvotes: 1

Views: 1918

Answers (1)

domis86
domis86

Reputation: 1357

For now, what I know is the polygon vertices

You probably draw your polygon using https://pixijs.download/dev/docs/PIXI.Graphics.html#drawPolygon method by passing to it a list of points - similar as last shape in this example: https://pixijs.io/examples/#/graphics/simple.js

// draw polygon
const path = [600, 370, 700, 460, 780, 420, 730, 570, 590, 520];

graphics.lineStyle(0);
graphics.beginFill(0x3500FA, 1);
graphics.drawPolygon(path);
graphics.endFill();

^ In that example we have 5 points: P (600, 370), Q (700, 460), R (780, 420), S (730, 570), T (590, 520).

It also means that we have 5 edges: PQ, QR, RS, ST, TP

Now, we should have some way to tell if mouse pointer "is hovered over some some edge". By "is hovered" i mean: it lies in some distance from edge - lets say said distance is 10 pixels. So we want to know if mouse pointer is 10 pixels away from some edge.

To know that we can use formula explained in Line defined by two points part in: https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line#Line_defined_by_two_points

  • P1=(x1,y1) and P2=(x2,y2) - are the beginning and end vertices of some edge (for example PQ)
  • (x0,y0) is our "mouse point"

You can iterate over all edges and perform above calculation - if the distance is less that 10 pixel for some edge then you have the answer. If there is more than one edge which meets this requirement then you should pick one with smallest distance (it can happen if for example mouse is placed near some vertice).

Now you have the selected edge. Now lets do following point from your question:

2. Then, when I hit the edge a small circle should appear

To calculate position of this circle we can use equation from same Wikipedia page: https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line#Line_defined_by_an_equation - the part The point on this line which is closest to (x0,y0) has coordinates:.

  • Here you need to convert coordinates of vertices from your selected edge to line function.

Then we can proceed to last point from your question:

3. And next I can drag and drop that part of the edge to creating a new point for the polygon

You can do it by adding new vertice to your polygon.

Lets assume that selected edge is PQ - then this new vertice should be added between vertices P and Q in the vertices list which you pass to drawPolygon method. Lets name this new vertice X. Coordinates of vertice X should be equal to current mouse coordinates.

Then you will have following edges: PX, XQ, QR, RS, ST, TP.

You probably want to activate this "mode" after mouse is clicked and when mouse button is down etc - but that is separate issue related to interactivity / GUI etc - not graphics :) .

Note: is good to separate your presentation part of application (graphics / pixi.js related things) from mechanics and interactivity / GUI etc. So for example: do your calculations in separate place (other class, method etc) from where you do your actual drawing (calling pixi.js methods, update canvas etc). Store results of calculations in some place (from above example it would be: list of vertices, position of circle, colors etc), and then when time comes to draw you take those results and just draw polygons using them. Dont mix everything in one place ;)

Upvotes: 1

Related Questions