Jump to content

Creating Polygon

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
coiner

coiner

    Newbie

  • Members
  • Pip
  • 4 posts
In java, I am trying to create an irregular Polygon of n sides. The side lengths and total perimeter are known. What is the best way to calculate the coordinates of this polygon?

#2
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

coiner said:

In java, I am trying to create an irregular Polygon of n sides. The side lengths and total perimeter are known. What is the best way to calculate the coordinates of this polygon?

Well, I don't know if this is what you wanted...
public double getMeasure(){

		for(int i = 0; i <= numOfPoints-2; i++){

			area += (xPoints[i]*yPoints[i+1])-(xPoints[i+1]*yPoints[i]);

		}

		area /= 2;

		if(area < 0)

			area *= -1;

		return area;

	}

Posted Image

#3
coiner

coiner

    Newbie

  • Members
  • Pip
  • 4 posts
No i do not want to calculate the area of a preexisting polygon, i want to construct an irregular polygon out of lines.

for example, the function looks like makePolygon(int nSides, int[] sideLengths)

how would i (mathematically) get the coordinates of the polygon's vertices?

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
There are multiple ways to make an irregular polygon, given only the side lengths. Are you looking for one with maximal area, does it need to be convex, etc?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
coiner

coiner

    Newbie

  • Members
  • Pip
  • 4 posts
i'm looking for maximal area but if you would like to share algorithms for convex as well i would appreciate it.

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
The easiest way I can think of to get a polygon is to start with a triangle with the same perimeter, where each side has length equal to the sum of a subset of the side lengths. Then you can start "distorting" one side at a time based on making one "line" a proper line. This will involve shifting vertices around to preserve side lengths.

The geometry of this is likely to get quite complicated. I suspect (but have not proven) that the optimal area will correspond to a polygon where the vertices are all on a circle. However, side length and arc length don't correlate terribly well, so I'm not sure how much that will help you construct the actual polygon.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
coiner

coiner

    Newbie

  • Members
  • Pip
  • 4 posts
That's a good idea and I will try it out. I can simply use this method and then check against what the total area should be. I'm sure with some tweaking I'll get it down. I'll post the results.

#8
kishkabear

kishkabear

    Newbie

  • Members
  • Pip
  • 5 posts
This is where trigonometry comes in... you can easilly move around a quadrant as long as you provide the sin and cos values as well as thye length of the move. You don't have to worry about closing the polygon, this is done automatically for you. Read more on the Polygon object in the Java API.

#9
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
I'm not that good with geometry (anymore). But Something with the max area covered will always approach a circle. So i'm wondering if you could get the size of the circle first. Put the first point on it. And then simply draw the next lines at the place where it will intersect with the circle, and if that turns out to be the largest area covered... Really interesting.... Gonna try this out next thursday / friday :D

Will see if it's even possible to find the size of the circle first...