Monday, December 10, 2018

adobe photoshop - Automating the Creation of Geometric Shapes?



How can I draw geometric shapes, or other regular shapes, such as a gear, using macros or scripts? I'd like to experiment with creating games by combining elements, similar to geoDefense: http://www.criticalthoughtgames.com



Answer



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 )

pentagon svg


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 )
star svg


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 )
gear svg


No comments:

Post a Comment

technique - How credible is wikipedia?

I understand that this question relates more to wikipedia than it does writing but... If I was going to use wikipedia for a source for a res...