• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Drawing a polygon
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Drawing a polygon


  • Subject: Drawing a polygon
  • From: Andrew Merenbach <email@hidden>
  • Date: Sat, 12 Oct 2002 11:32:21 -0700

I worked out my very first NSView drawing code a few days ago, and I'm quite pleased with it. Since it is my first time drawing in Cocoa, though, I would like to know I made things more complicated than they had to be.

My program is a geometry calculator: the user enters the number of points in a polygon, and the program figures out various things about it. I wanted to add some code to draw the polygon in an NSView in my window, so I did the following:

1. I added the NSView to the window, and made it a custom type (GMPolygonView).
2. To give the view a bezel, I made it a subview of a scroll view without scrollbars.
3. When the user enters the number of points in the polygon, I detach a thread to draw the polygon:
(Note: a regular polygon with any number of sides can be drawn inside of a circle if the points are spaced an equal distance apart.)
a. Divide the number of degrees in a circle (360) by the number of points in the polygon. This is the interval of degrees between each poin
b. Create an NSBezierPath that draws invisible arcs with the same number of degrees as the result of (a), marking the end of each one.
c. Iterate through the *points* collected in (b) and add them (and not the arcs) to a second bezier path, where they will be connected and drawn.

This works very well, but is a bit slow for huge polygons (those with more than several thousand sides). This is not a big issue, as I detach a thread for the drawing, and I wanted to keep the interface consistent (I'd feel a bit guilty about just drawing a circle for polygons with more than 360 sides, for instance, especially if my program ever evolves to manipulate the polygons it draws in other ways).

If anyone is interested in my code--either to check it for efficiency or to use it in their own programs--I would be happy to share it. Also, if anyone sees a more efficient way of drawing a polygon with an arbitrary number of sides, or already has code to do this, I am interested in hearing of it (for instance, I considered calculating the points in the circle with sines and cosines, but decided to just use bezier paths so I wouldn't need to convert between degrees and radians).

Take care,
Andrew Merenbach
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Drawing a polygon
      • From: Scott Anguish <email@hidden>
  • Prev by Date: Two ways to get this NSTableView behaviour but I do not know how to implement either
  • Next by Date: Re: Two ways to get this NSTableView behaviour but I do not know how to implement either
  • Previous by thread: Re: Two ways to get this NSTableView behaviour but I do not know how to implement either
  • Next by thread: Re: Drawing a polygon
  • Index(es):
    • Date
    • Thread