Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's fairly straightforward to do this for regular polygon.</p> <p>The area of an n-sided regular polygon, with a circumscribed circle of radius R is</p> <p><code>A = 1/2 nR^2 * sin((2pi)/n)</code></p> <p>Therefore, knowing n and A you can easily find R</p> <p><code>R = sqrt((2*A)/(n*sin((2pi)/n))</code></p> <p>So, you can pick the center, go at distance R and generate n points at <code>2pi/n</code> angle increments.</p> <p>In R:</p> <pre><code>regular.poly &lt;- function(nSides, area) { # Find the radius of the circumscribed circle radius &lt;- sqrt((2*area)/(nSides*sin((2*pi)/nSides))) # I assume the center is at (0;0) and the first point lies at (0; radius) points &lt;- list(x=NULL, y=NULL) angles &lt;- (2*pi)/nSides * 1:nSides points$x &lt;- cos(angles) * radius points$y &lt;- sin(angles) * radius return (points); } # Some examples par(mfrow=c(3,3)) for (i in 3:11) { p &lt;- regular.poly(i, 100) plot(0, 0, "n", xlim=c(-10, 10), ylim=c(-10, 10), xlab="", ylab="", main=paste("n=", i)) polygon(p) } </code></pre> <p>We can extrapolate to a generic convex polygon.</p> <p>The area of a convex polygon can be found as: <code>A = 1/2 * [(x1*y2 + x2*y3 + ... + xn*y1) - (y1*x2 + y2*x3 + ... + yn*x1)]</code></p> <p>We generate the polygon as above, but deviate angles and radii from those of the regular polygon.</p> <p>We then scale the points to get the desired area. </p> <pre><code>convex.poly &lt;- function(nSides, area) { # Find the radius of the circumscribed circle, and the angle of each point if this was a regular polygon radius &lt;- sqrt((2*area)/(nSides*sin((2*pi)/nSides))) angle &lt;- (2*pi)/nSides # Randomize the radii/angles radii &lt;- rnorm(nSides, radius, radius/10) angles &lt;- rnorm(nSides, angle, angle/10) * 1:nSides angles &lt;- sort(angles) points &lt;- list(x=NULL, y=NULL) points$x &lt;- cos(angles) * radii points$y &lt;- sin(angles) * radii # Find the area of the polygon m &lt;- matrix(unlist(points), ncol=2) m &lt;- rbind(m, m[1,]) current.area &lt;- 0.5 * (sum(m[1:nSides,1]*m[2:(nSides+1),2]) - sum(m[1:nSides,2]*m[2:(nSides+1),1])) points$x &lt;- points$x * sqrt(area/current.area) points$y &lt;- points$y * sqrt(area/current.area) return (points) } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload