Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>How general does this need to be? Also, how are representing your shape, and the points? Your algorithm seems to be ok; do you need help turning it into code?</p> <hr> <p>Alrighty, here's something i came up with. Notes on the code: </p> <ul> <li>The distance method takes two Points and returns the distance between them.</li> <li>The normalize method takes two points and returns the normal vector pointing from the first point to the second point.</li> <li>The Point class has the multiply method which multiplies the point by a scalar</li> <li>The Point class has float (or double) precision</li> </ul> <p>I'm using the Point class to represent vectors by the way.</p> <p>I haven't tested this, so there might be bugs. There might be issues with how this algorithm handles exact regions (e.g. your square with exactly 4 points on it). Let me know if there are issues or if you have any questions! :)</p> <pre><code>Point[] shapePoints; //already initialized int numPoints; //already initialized Point[] retPoints = new Point[numPoints]; int totalLength; for(int i = 1; i &lt; shapePoints.length; i++){ totalLength += distance(shapePoints[i], (shapePoints[i-1])); } float segLength = ((float) totalLength) / numPoints); Point currShape = shapePoints[0]; Point nextShape = shapePoints[1]; Point prev = currShape; int counter = 2; while(numPoints &gt; 0){ Point norm = normalize(new Point(nextShape.x - currShape.x, nextShape.y - currShape.y)); if(distance(nextShape, prev) &lt; segLength){ int tempLength = segLength; tempLength -= distance(nextShape, prev); currShape = nextShape; nextShape = shapePoints[counter]; counter ++; norm = normalize(new Point(nextShape.x - currShape.x, nextShape.y - currShape.y)); norm.multiply(tempLength); } else{ norm.multiply(segLength); } retPoints[numPoints - 1] = norm; prev = retPoints[numPoints - 1]; numPoints --; } </code></pre> <hr> <pre><code>Point normalize(Point p){ int scale = Math.sqrt(p.x * p.x + p.y * p.y); p.x = p.x / scale; p.y = p.y / scale; return p; } </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