Augusto - 1 year ago 63
Java Question

# Get the X,Y coordinate at the edge of a rhombus, based on the rhombus' width and angle

I'm developing a Processing (Java) sketch that, given a certain angle, draws a dot at the edge of a rhombus.

I know the width of the rhombus, and its position, but I'm not sure how to calculate the x-y coordinates of a dot resting at its edge.

Are there any elegant solutions for this problem? Any help in pseudocode would be welcomed.

Let's square side length is `A`, half-length is `H = A/2`. Angle `Theta`. Intersection point P.
All coordinates are relative to the square center.

Rotate square by `-Pi/4`, angle `Alpha = Theta - Pi/4`

``````if Alpha lies in range -Pi/4..Pi/4, then intersection point P' = (H, H*Tan(Alpha))
if Alpha lies in range Pi/4..3*Pi/4, then P' = (H*Cotangent(Alpha), H)
if Alpha lies in range 3*Pi/4..5*Pi/4, then P' = (-H, -H*Tan(Alpha))
if Alpha lies in range 5*Pi/4..7*Pi/4, then P' = (-H*Cotangent(Alpha), -H)
``````

Then rotate point `P'` back by `Pi/4`:

``````S = Sqrt(2)/2
P.X = S * (P'.X - P'.Y)
P.Y = S * (P'.X + P'.Y)
``````

``````A = 200, Theta = 5*Pi/12