Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-26-2011, 10:18 AM   #1 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 118
lexy0202 is on a distinguished road
Default [SOLVED] Quartz and Polygons??

Hi,

I've just started learning about Quartz and it's great, except I still don't know very much about it. I've been on a couple of tutorials, plus one in 'Beginning iPhone Games Development', and I can add rects, sphere's ellipses all filled with any color... that's all fine.

However what I want to do is create an app with a slider on the bottom half of the screen and and a UIView on the top half which will be subclassed to be my QuartzView, then in there, when the slider changes I want to update it with a polygon with that number of sides, (between 3 and 20). I'm hoping that I could do this mathematically rather than have a massive switch... - Also, I want it to be just an outline, not filled, in black.

Could someone explain basically how to do this.

I've heard about Stanford's helloPoly, however the source code is ancient and won't run on my mac.

Cheers,
Alex

Last edited by lexy0202; 05-26-2011 at 01:43 PM.
lexy0202 is offline   Reply With Quote
Old 05-26-2011, 10:27 AM   #2 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Did you check out the QuartzDemo: Loading… ?
baja_yu is offline   Reply With Quote
Old 05-26-2011, 10:30 AM   #3 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 118
lexy0202 is on a distinguished road
Default

Thanks, I had, but that only shows a star, and a hexagon...
lexy0202 is offline   Reply With Quote
Old 05-26-2011, 10:39 AM   #4 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 118
lexy0202 is on a distinguished road
Default

Been playing around with Apple's code for making polygons:

[code]
CGPoint center;
// Now add the hexagon to the current path
center = CGPointMake(210.0, 90.0);
CGContextMoveToPoint(context, center.x, center.y + 60.0);
for(int i = 1; i < 6; ++i)
{
CGFloat x = 60 * sinf(i * 2.0 * M_PI / 6.0);
CGFloat y = 60 * cosf(i * 2.0 * M_PI / 6.0);
CGContextAddLineToPoint(context, center.x + x, center.y + y);
}
[code]

I figured, in the bit that says (i * 2.0 * M_PI / 6.0);
if I change the 2.0 to a 3.0, it becomes a square on its side. If I change it to a 4, it becomes a triangle (the right way up).

My question is, what is the logic behind this code?

Thanks,
Alex
lexy0202 is offline   Reply With Quote
Old 05-26-2011, 10:42 AM   #5 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 118
lexy0202 is on a distinguished road
Default

OK sorry to keep posting but just realised that the last number in that line of code is the number of sides.

Only problem now is that the rotation goes skew... e.g. the square is a diamond and the triangle is upside down, how do I fix this?
lexy0202 is offline   Reply With Quote
Old 05-26-2011, 12:01 PM   #6 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 118
lexy0202 is on a distinguished road
Default

Almost there, hopefully...

Here is what happens if I choose a triangle:


So that's fine...

Here is what happens if I choose a square:


If a bit rotated, fine...

All the others work fine (no funky rotations), up to a septagon...


then they all clip, and here is a 20-sided polygon (according to my code):


There is a probably a very simple solution to my problem, and here is my drawRect code for that view:

Code:
- (void)drawRect:(CGRect)rect
{
    //Getting number of sides
    CGFloat sides = self.numOfSides;
    NSLog(@"%g", sides);
    //Initial setup
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    
    CGAffineTransform t0 = CGContextGetCTM(context);
    t0 = CGAffineTransformInvert(t0);
    CGContextConcatCTM(context, t0);
    
    //Begin drawing setup
    CGContextBeginPath(context);
    CGContextSetRGBStrokeColor(context, 0, 0, 0, 1);
    CGContextSetLineWidth(context, 2.0);
    
	CGPoint center;
    
    //Start drawing polygon
    center = CGPointMake(90.0, 90.0);
	CGContextMoveToPoint(context, center.x, center.y + 60.0);
	for(int i = 1; i < 6; ++i)
	{
		CGFloat x = 60 * sinf(i * 2.0 * M_PI / sides);
		CGFloat y = 60 * cosf(i * 2.0 * M_PI / sides);
		CGContextAddLineToPoint(context, center.x + x, center.y + y);
	}
    
    //Finish Drawing
    CGContextClosePath(context);
    CGContextDrawPath(context, kCGPathStroke);
    
    CGContextRestoreGState(context);
}
Thanks in advance,
Alex
lexy0202 is offline   Reply With Quote
Old 05-26-2011, 01:43 PM   #7 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 118
lexy0202 is on a distinguished road
Default

Apologies, solved - check the for loop...
lexy0202 is offline   Reply With Quote
Old 05-26-2011, 01:50 PM   #8 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by lexy0202 View Post
Apologies, solved - check the for loop...

Glad to see that you figured this out.

Cocoa touch now has UIBezierPaths, which are easier to use than CG paths. If your goal is to do this stuff easily, I'd suggest looking into UIBezierPaths. If you really want to use the CG calls, by all means, have at it.


EDIT: Do you understand what the code you posted is doing?

The for loop steps through all the vertexes in your polygon. (1 through number_of_sides.)

The code calculates the angle from the center of the polygon to that vertex, and then uses sine and cosine to calculate the x & y offset for a point at that angle that lies on the circle that your polygon inscribes.

I'm working on a kaleidoscopes app that creates polygon-shaped kaleidoscopes (among others) and it uses code that's just about identical to the code you posted. I'm using OpenGL, so the actual drawing code is different, but the math to calculate the vertexes is the same.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.

Last edited by Duncan C; 05-26-2011 at 02:29 PM.
Duncan C is offline   Reply With Quote
Old 05-26-2011, 01:56 PM   #9 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 118
lexy0202 is on a distinguished road
Default

Cheers duncan... just wondering because I'm also creating something for which I'm going to need to display a specific segment of a circle (arc, plus the two sides)... can UIBezierPaths do that??
lexy0202 is offline   Reply With Quote
Old 05-26-2011, 02:01 PM   #10 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by lexy0202 View Post
Cheers duncan... just wondering because I'm also creating something for which I'm going to need to display a specific segment of a circle (arc, plus the two sides)... can UIBezierPaths do that??
You should be able to do everything you can do with CGPaths with UIBezierPaths. They are just easier to work with because they are standard NSObjects.

I have been using CGPaths since iOS was introduced, and only discovered UIBezierPaths recently. They were added in iOS 3.2 (With the iPad, I guess.)
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 05-26-2011, 02:02 PM   #11 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 118
lexy0202 is on a distinguished road
Default

Good tutorial suggestion??

Thanks,
Alex
lexy0202 is offline   Reply With Quote
Old 05-26-2011, 02:05 PM   #12 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by lexy0202 View Post
Good tutorial suggestion??

Thanks,
Alex

Let me google that for you
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 05-26-2011, 02:09 PM   #13 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 118
lexy0202 is on a distinguished road
Default

Ty but most of them are blocked at my school will have to wait for couple of days!
lexy0202 is offline   Reply With Quote
Old 05-26-2011, 03:54 PM   #14 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

Quote:
Originally Posted by lexy0202 View Post
Ty but most of them are blocked at my school will have to wait for couple of days!
click "copy cache" from google, you will bypass the limitation using google as a proxy...
__________________
dany_dev is offline   Reply With Quote
Reply

Bookmarks

Tags
drawing, graphics, quartz, uiview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 345
6 members and 339 guests
doffing81, dre, iOS.Lover, jenniead38, Kirkout, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:11 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0