Plot rectangle points given height, width, number points

182 Views Asked by At

Is it possible to plot the points of a rectangle given the width, height and number of points all within a for-loop using sin/cos or something similar. I'm trying to find an easy way to plot these points in consecutive order.

One thing to mention is that the points would always be a multiple of 4.

enter image description here

1

There are 1 best solutions below

0
On

I assume that by "consecutive order", you mean as one travels around the perimeter. Sine and cosine are not well suited for this. The angles between adjacent points change as you go around.

The simplest way is just to use 4 loops: Starting that (x0, y0) as the lowest coordinate corner, with width w and height h, and having n + 1 points per side (counting both corners, so n = 4 in your example), you can do:

dw = w / n
dh = h / n
x1 = x0 + w
y1 = y0 + h
for i = 0 to n - 1
    plot (x0 + i * dw, y0)
next i
for i = 0 to n - 1
    plot (x1, y + i * dh)
next i
for i = 0 to n - 1
    plot (x1 - i * dw, y1)
next i
for i = 0 to n - 1
    plot (x0, y1 - i * dh)
next i