Re: Wedgies anyone ?
Re: Wedgies anyone ?
- Subject: Re: Wedgies anyone ?
- From: Richard <email@hidden>
- Date: Mon, 17 Jun 2002 17:02:21 +1000
- Organization: Faraday R&D
>
--__--__--
>
>
Message: 3
>
Date: Sun, 16 Jun 2002 11:34:36 -0400
>
Subject: Wedgies anyone ?
>
From: Robert Miller <email@hidden>
>
To: Cocoa Development <email@hidden>
>
>
Hello,
>
>
Help please. I'm having trouble drawing a wedge (or a piece of pie
>
if you will) using NSBezierPath, this is probably a basic trig thingy
>
and I just haven't hit on it. I have a circle with radius 'R' so I move
>
to the center point of the circle. I calculate the starting point on the
>
circle like so... here's a code snippet....
>
>
// arc slice is the percentage of the total of the circle in radians
>
arcSlice = (num / chartTotal) * piTimes2;
>
>
startAngle = arcSum + 1 / pieRadius;
>
endAngle = (arcSum + arcSlice) + 1 / pieRadius;
>
>
startPt = NSMakePoint(pieCenter.x + pieRadius * sin(startAngle),
>
pieCenter.y + pieRadius * cos(startAngle));
>
endPt = NSMakePoint(pieCenter.x + pieRadius * sin(endAngle),
>
pieCenter.y + pieRadius * cos(endAngle));
>
>
arcStartPt = NSMakePoint(pieCenter.x + sin(startAngle), pieCenter.y +
>
cos(startAngle));
>
arcEndPt = NSMakePoint(pieCenter.x + sin(endAngle), pieCenter.y +
>
cos(endAngle));
>
>
path = [NSBezierPath bezierPath];
>
[path moveToPoint:pieCenter];
>
[path lineToPoint:startPt];
>
[path appendBezierPathWithArcFromPoint:arcStartPt toPoint:arcEndPt
Try this:
For a start: x goes with cos and y goes with sin.
But: the main problem is that the documentation for appendBezierPathWithArcFromPoint sucks
and I had to experiment with some interactive mouse code to figure out how it interprets
the 3 points it needs to draw the arc.
You would be better off invistigating some of the
other arc drawing functions.
- (void)
drawRect: (NSRect) inRect
{
float startAngle = 0.0;
float totalAngle = 0.0;
float halfSegmentAngle = 0.0;
float segmentAngle = 0.0;
float pieSegments[] = {0.1, 0.4, 0.3};
float pieTotal = 1.0;
NSPoint beginArc, endArc, midArc;
NSPoint pieCentre = {100.0, 100.0};
float pieRadius = 50.0;
NSRect boundsRect = [self bounds];
//NSPoint origin = boundsRect.origin;
[[NSColor greenColor] set];
[NSBezierPath fillRect: boundsRect];
[[NSColor whiteColor] set];
// read value from user's slider control
// and scale it.
segmentAngle = mPieAngle*4;
// Use this value for the segment value if i have a pie graph to plot.
//segmentAngle += (pieSegments[0]/pieTotal)*2.0*3.14159;
halfSegmentAngle = 0.5*segmentAngle;
// The starting angle for the pie graph is some arbitrary value.
// The total angle is the angle of the current ray that separates
// one pie segment from the next.
// hence its initial value is just the start angle.
totalAngle += startAngle;
// Coordinates of the endpoint of the first ray.
beginArc.x = pieCentre.x + pieRadius* cos(totalAngle);
beginArc.y = pieCentre.y + pieRadius* sin(totalAngle);
// Coordinates of the bezier control point (I guess) for the arc.
// This point is at the intersection of the two tangents
// produced from each end of the arc but more importantly here
// it is on the bisector of the pie segment.
midArc.x = beginArc.x - pieRadius*tan(halfSegmentAngle)*sin(totalAngle);
midArc.y = beginArc.y + pieRadius*tan(halfSegmentAngle)*cos(totalAngle);
// Cooedinates of the endpoint of the second ray.
totalAngle += segmentAngle;
endArc.x = pieCentre.x + pieRadius* cos(totalAngle);
endArc.y = pieCentre.y + pieRadius* sin(totalAngle);
// Erase the previous path and build a new path using an arc
// and two rays to outline the pie segment.
[mPath release];
mPath = [[NSBezierPath alloc] init];
[mPath moveToPoint: pieCentre];
[mPath lineToPoint: beginArc];
// Note the previous lineToPoint sets up an additional point used in
// the arc drawing routine which comes next.
[mPath appendBezierPathWithArcFromPoint: midArc toPoint: endArc radius: pieRadius];
[mPath moveToPoint: pieCentre];
[mPath lineToPoint: endArc];
[mPath closePath];
[mPath stroke];
}
_______________________________________________
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.