# How to collect the clockwise and anticlock wise points from reference points

Hello All,

Please i need urjent help on this. I want to determine a way to collect the clockwise points from my reference point A and anticlock wise points also.

Please see the picture that is self explainatory.

@prisoner849 , i think you may have some solution for this problem, I have hope

s from you

If you have 3 or more points, mentioned in a specific order, you can make a polygon out of them and determine if it’s wound cw or ccw ( by calculating z component of a cross product). So [a, b, e] will be wound ccw and [a, e, b] - cw.

Firstly thank you so much for replying.

In here in the above image i already have a polygon and A and B are the points that divide the polygon into two polygons.

So here i need a way to collect all the points in clockwise direction from point A to point B than all the point in anticlock wise direction from point A to point B

So on the basis of these points i can draw polygon and here one more thing i need to keep the order same as in the picture in the array to keep the shape proper.

Thanks

If you don’t know the order of points, then it’s hard to tell.
I’m not sure I understand, but if by “collect” you mean “while following the curve from A to B” then you need to follow the curve (mathematically). Basically, if you erase the curve from your picture, how do you know in which order to connect the points to make the polygon, there can be many combinations?
If you know the order, then you already can make two polygons between A and B, one cw and one ccw. Then you can check which one is which.

Can you share a rough algo

see my points are like this initially
[a,b,c,d,e]

Than after adding A and B i have
[a,A,b,c,d,e,B]

How how i can get cw and acw
Thanks

Roughly, something like this:

``````const polygonWinding = pts => {
const len = pts.length;
let [minx, miny, mi] = [pts, pts, 0];
for (let i = 1; i < len; i++) {
const [pix, piy] = [pts[i], pts[i]];
if (pix < minx || pix == minx && piy < miny) [minx, miny, mi] = [pix, piy, i];
}
const [mp, mn] = [mi != 0 ? mi - 1 : len - 1, mi != len - 1 ? mi + 1 : 0];
const [a, b, c] = [pts[mp], pts[mi], pts[mn]];
const det = (b - a) * (c - a) - (c - a) * (b - a);
return det < 0 ? 1 : -1; // cw : ccw
};
``````

so
`polygonWinding([[0,0], [1,1], [1, 2]]) == -1` ccw
`polygonWinding([[0,0], [1,1], [0, -1]]) == 1` cw

in your original drawing you call twice with [a, q, B] and [a,b,c,d,e,B]

We also have this in the framework: three.js docs

2 Likes

Thanks but this can be done by Shape Utils also.

You dont get the problem.

Problem is we have a let say rectangle it is formed by a polyline. Now i have 4 points in the polyline

{0,0} {10,0} {10,10} {0,10}

Now let say if i want to break this into two parts

see the drawing

Now i need a algo that give me two polygon

{0,0} {5,0}{10,5}{10,10}{0,10}

{5,0} {5,5} {10,5}{10,0}

So i am looking how i can achieve that

I tried sutherland but it give me clipped area and secondly i only works with clipping window.

So i am trying to make my own

@prisoner849 its like your offset problem only. Thing is i can draw any interest on the offset to break it.

You write to my article is like great hope. And thanks to @tfoller as well

@Alex IIRC, you’ve been provided with some suggestions for subtraction/splitting of a polygon: Splitting of shapes - #2 by vielzutun.ch
Have you tried something?

Yeah , this algorithm is for polygon. And i dont need it this way.

So actually it pick the area of the polygon that is in the clipping window and remove other part but i need splitting not clipping.

Its already a built in sol that works with bounding box

But see this picture my users can do things like this and i believe its just arranging the points