# How do I Find the edges of a square?

Get help using Construct 2

### » Sun Jul 24, 2016 1:44 am

I want to find the edges of a square depends on the position of the player.
Here is a picture of what I'm talking about.

1, 2, 3 and 4 are ImagePoints. So depends on the player's position, the edges are different. In the left one, 3 and 4 are the edges, In the middle 3 and 2 and in the right 1 and 4 are the edges. There are overall 8 possibilities, I just showed 3 of them. I would appreciate if someone make a capx file and uplaod it.

Thank you
Last edited by HessamoddinS on Mon Jul 25, 2016 7:09 pm, edited 2 times in total.
B
46
S
19
G
3
Posts: 105
Reputation: 4,634

### » Sun Jul 24, 2016 8:11 am

Tricky.......... here is my attempt. https://www.dropbox.com/s/vbn9xluozgmf8 ... .capx?dl=0

I don't know if this is a good way to go about it though, in terms of efficiency or logic.

I've saved each imagepoints' angle difference from the center point to an array, then sorted the array to get the two points with the largest difference.

EDIT: Updated original capx to clean it up, and now also works for any number of imagepoints/vertices.
B
57
S
28
G
115
Posts: 1,712
Reputation: 65,172

### » Sun Jul 24, 2016 5:38 pm

oosyrag wrote:Tricky.......... here is my attempt. https://www.dropbox.com/s/vbn9xluozgmf8 ... .capx?dl=0

I don't know if this is a good way to go about it though, in terms of efficiency or logic.

I've saved each imagepoints' angle difference from the center point to an array, then sorted the array to get the two points with the largest difference.

EDIT: Updated original capx to clean it up, and now also works for any number of imagepoints/vertices.

Thank you, this is what I needed.
B
46
S
19
G
3
Posts: 105
Reputation: 4,634

### » Mon Jul 25, 2016 4:02 pm

One slightly different way would be to get the angle from each imagepoint to the player and subtract from that the angle to the next imagepoint.

Or in the simple case of a square and the imagepoints are layed out in this order:
41
32
It simplifies to:
set a to angle(Target.ImagePointX(loopindex+1),Target.ImagePointY(loopindex+1),Sprite.X,Sprite.Y) -90*loopindex

Then if the following is true for any imagepoint you can create it.
(-sin(a)<0 & cos(a)>0) | (cos(a)<0 & -sin(a)>0)
B
95
S
34
G
128
Posts: 5,493
Reputation: 81,674

### » Mon Jul 25, 2016 6:00 pm

Made a capx for myself to help understand/visualize above approach, leaving it here.

https://www.dropbox.com/s/stuquplu2gph1 ... .capx?dl=0
B
57
S
28
G
115
Posts: 1,712
Reputation: 65,172

### » Mon Jul 25, 2016 6:05 pm

Thanks guys, great solutions, really helped to solve my problem.
Last edited by HessamoddinS on Mon Jul 25, 2016 7:05 pm, edited 1 time in total.
B
46
S
19
G
3
Posts: 105
Reputation: 4,634

### » Mon Jul 25, 2016 6:36 pm

Marked this, so i can find it back. Sorry to bother.
B
33
S
18
G
29
Posts: 2,493
Reputation: 21,450

### » Mon Jul 25, 2016 8:08 pm

I guess it would probably be haelpful to expalin the formula.
Here's a general view for one corner. P1 is the current corner and P0,P2 are the previous and next corners. "A" is the location of the player.
Code: Select all
`   A  / /P1----P2|     ||     ||     |P0----+So next I made the obsevation that the current point (A) would only need to be created if it's in either of these two regions (b or c).     .........     .........     bbbbbbb..     bbbbbbb..     bbbbbbb....cccP1---+..ccc|    |..ccc|    |..ccc+----+..ccc..........To calculate which region I used a vector dot product to calculate a vector projection.  That sounds more vague than it is but it looks like this. It works relative to any corner.(A-P1) dot (P0-P1) < 0 and (A-P1) dot (P2-P1) > 0or(A-P1) dot (P2-P1) < 0 and(A-P1) dot (P1-P1) > 0`

The rest was a lot of math simplification.
B
95
S
34
G
128
Posts: 5,493
Reputation: 81,674