Take the 2-minute tour ×
Graphic Design Stack Exchange is a question and answer site for Graphic Design professionals, students, and enthusiasts. It's 100% free, no registration required.

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

share|improve this question
1  
Which software? The solutions will differ from app to app. Should your game create the shapes dynamically rather than creating the shapes in a graphic editing software beforehand? You can't create games with ia. Photoshop, maybe Flash would be better option? (…and in the last two cases, this question is probably quite off–topic for the site) –  koiyu Apr 13 '11 at 10:50
    
I have Photoshop, Gimp and InkScape on a Mac. Anyone will do. From the basic shapes, I'll try to compose other images. Flash doesn't work on iPhone/iPad. –  user1038 Apr 13 '11 at 12:14
    
I should add that I recently bought Photoshop so I would like to learn it well. –  user1038 Apr 13 '11 at 12:22
    
What exactly do you need to script? If you merely need to create a shape, would saving a shape as a PSD be sufficient? You could consider it as a template and later save your edits as a new file. –  koiyu Apr 13 '11 at 12:37
1  
I rather ask for clarifications than just vote to close. First, I tried to figure out which tool you're intending to use. Second, I tried to figure out whether your problem can be solved in other manners (contra answering the question you provided), as I haven't got too much of an experience with Photoshop scripting. I know it can be scripted with AppleScript, VBScript and JavaScript. Adobe Developer Connection page might be one of the best places to start: adobe.com/devnet/photoshop/scripting.html I hope someone can provide a more thorough answer to your question. –  koiyu Apr 13 '11 at 15:39
show 3 more comments

3 Answers 3

up vote 1 down vote accepted

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

share|improve this answer
add comment

Please note this answer addresses both the question and the comments...

Some things just aren't easily scriptable. A gear isn't a "regular" shape. Basic shapes—squares, circles, and the like—are so simple to create in the apps you mention that a script isn't necessary unless you plan on doing some complex transformations with the shape afterwards. A gear, on the other hand, really isn't a scripted item in a graphics application; an image of a gear is made by hand, or a pre-existing image altered to suit particular needs, then exported to the required format per your project's spec and used accordingly during runtime.

You don't mention how far you are in planning your application, but I suggest you look at your project specs and start looking at how you intend to use the art in question, because your implementation will drive how and what you create in a graphics application if you create anything in Photoshop at all. Your framework of choice (per your example would most likely be Core Graphics or Cocos2D) may already have something to offer in terms of ready-made functions to create and manipulate simple to moderately-complex shapes.

Gears are common to games, not graphics applications. Graphics applications are more about providing the core tools needed to make any kind of gear imaginable. Games invariably require custom graphics that aren't easily scripted until you have a clear idea in mind of what you want to make, and even then, the workflow would still more than likely be similar to what @koiyu described.

Also, take into consideration that it may be (more than likely will be) faster to simply draw the gear by hand (or hire someone) than to find a programmatic way to do this.

share|improve this answer
    
Not automated but I found how to draw a gear in Inkscape: wikihow.com/Draw-Gears-in-Inkscape –  user1038 Apr 13 '11 at 18:26
    
I don't understand when you write that gears are not "regular shapes. What is your definition of "regular"? I made and saw programs drawing gears. One of the interests of such script would be to define the number of teeth, the size, etc. –  PhiLho Jul 27 '11 at 12:12
    
@PhiLho: "regular" is, obviously, a subjective term. In my context for this question, which is largely driven by Adobe Illustrator, gears are not a part of the standard tool set or in the default plug-ins, at least for what I do. Creating gears automagically is something that has to be specially written. I've needed rounded rectangles a lot more than I have needed gears. –  Philip Regan Jul 27 '11 at 13:34
    
@Philip: Ah, so it was in the sense of "common", not in the sense of "with regularity in the form"... :-) Indeed, it is something that must be scripted, that's the point of the thread, no? ;-) –  PhiLho Jul 27 '11 at 16:09
    
@PhiLho: Yes, this question is about scripts, but I don't know of any off the top of my head, and I'm not about to write one. –  Philip Regan Jul 27 '11 at 23:32
add comment

Read the manuals?

Gimp can be scripted in Scheme (and some other languages like Lua). Inkscape can be scripted in Python, I think. Photoshop can be scripted in its own language (VBA?) too.

Beside, you can use any language to generate SVG files (assuming you learn the format...), and most of them (with the proper libraries) to generate bitmap images.

[Update] OK, I did the requested research... Two interesting Gimp scripts, one in Scheme, one in Python, coming with source code for further studying:
Shape Paths
Gimp Path Tools

They are not really simple... But the hard part, coding, is done so that lot of shapes can be done without even coding.

share|improve this answer
1  
Your answer really doesn't offer anything concrete. Can you provide a solution in Scheme, for example, to draw a geometric shape? People come to sites like this to get answers that will save them time. –  user1038 Jul 27 '11 at 13:05
1  
I don't see the value of giving a code snippet that applies only to a very specific case. Your question is about programming. That's something you must learn (programming, or if you know that already, the specific language & API). –  PhiLho Jul 27 '11 at 16:07
    
I didn't even think about script-created SVGs. That's an excellent suggestion... –  Farray Jul 27 '11 at 17:55
    
Ugh. Personally, I would remove the "Read the manuals?" and "LMGTFY". The point of these wiki sites is to gather the information and present it in a coherent, comprehensive and succinct way so that when the next person Googles "How do I ...", he gets an answer more quickly. Many answers are already buried out there but this wiki site is often a better format. All questions from trivial to hard should be here.Btw, the Shape Paths looks promising. Downloading Gimp now to try the plugin. –  user1038 Jul 29 '11 at 3:12
1  
@M-x If you'd like to shape the direction that GD is going, please weigh in over at meta. As it stands right now, most of the SE sites tend to shy away from overly-trivial questions/answers and management has stated "we're not a snippet site". Not that those answers can't find a home here, but it's not really what you should inherently expect to find. –  Farray Jul 29 '11 at 5:54
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.