Mobile app version of vmapp.org
Login or Join
Berumen635

: It sounds like you're looking for snippet code in Scheme. This really isn't the place to find that. That aside... Simple Polygons (triangles, squares, pentagons, & so-on) These shapes

@Berumen635

It sounds like you're looking for snippet code in Scheme. This really isn't the place to find that. That aside...



Simple Polygons (triangles, squares, pentagons, & so-on)

These shapes are the easiest. All you need to do is find evenly distributed points along the circumference of a circle. There is 1 point per side, so a triangle will have 3 points on a circumference, a square will have 4, etc.

The seemingly "scary" part is getting the circumference coordinates but it's actually pretty simple. Here's some pseudocode:

GetEndPoint( startX, startY, radius, angle )
{
endX = startX + ( radius * Sin( angle * PI ) )
endY = startY + ( radius * -Cos( angle * PI ) )
return endX, endY
}


Now that you have a simple way to find points, all you have to do is iterate your way around the circumference and draw a line from point to point.

poly( centerX, centerY, radius, sides )
{
incrementAngle = 360 / sides

currentAngle = 0
while ( currentAngle < 360 )
{
coordinates = GetEndPoint( centerX, centerY, radius, currentAngle )
DrawCoordinates( coordinates )
currentAngle += incrementAngle
}
}


If you combine these concepts with PhiLho's suggestion about creating SVGs, you can very easily script creation of a polygon.

poly( x, y, 50, 5 )


Stars

Stars are only slightly more complicated than polygons. The process is the same, but you have 2 radii to deal with which means you'll have twice as many points. Notice that you need to use a smaller incrementing angle and you're adjusting it for each radii on each loop.

star( centerX, centerY, radius1, radius2, points )
{
incrementAngle = 180 / points

currentAngle = 0
while ( currentAngle < 360 )
{
coordinates = GetEndPoint( centerX, centerY, radius1, currentAngle )
DrawCoordinates( coordinates )
currentAngle += incrementAngle

coordinates = GetEndPoint( centerX, centerY, radius2, currentAngle )
DrawCoordinates( coordinates )
currentAngle += incrementAngle
}
}


Again with PhiLho's excellent SVG idea:

star( x, y, 50, 20, 30 )


Gears

Once you've figured out stars, you can apply the same concept to gears. You have 2 radii and you just have to alternate drawing 2 consecutive points on each circumference.

gear( centerX, centerY, radius1, radius2, teeth)
{
incrementAngle = 90 / teeth

currentAngle = 0
while ( currentAngle < 360 )
{
coordinates = GetEndPoint( centerX, centerY, radius1, currentAngle )
DrawCoordinates( coordinates )
currentAngle += incrementAngle

coordinates = GetEndPoint( centerX, centerY, radius1, currentAngle )
DrawCoordinates( coordinates )
currentAngle += incrementAngle

coordinates = GetEndPoint( centerX, centerY, radius2, currentAngle )
DrawCoordinates( coordinates )
currentAngle += incrementAngle

coordinates = GetEndPoint( centerX, centerY, radius2, currentAngle )
DrawCoordinates( coordinates )
currentAngle += incrementAngle
}
}


One more tip of the cap to PhiLho:

gear( x, y, 50, 46, 24 )

10% popularity Vote Up Vote Down


Login to follow query

More posts by @Berumen635

0 Comments

Sorted by latest first Latest Oldest Best

Back to top | Use Dark Theme