drawRect not responding - iphone

I am trying to draw a pentagon with a set of points storing in a NSArrary, however, the view is empty without any error...
-(void)prepareVertex:(int)noOfVertex
{
polygonPoint=[NSMutableArray arrayWithCapacity:noOfVertex];
for(int i=0;i<noOfVertex;i++)
{
CGPoint point=CGPointMake(sin((2*M_PI)*i/noOfVertex),(cos(2*M_PI)*i/noOfVertex));
[polygonPoint addObject:[NSValue valueWithCGPoint:point]];
NSValue *tempVal=[polygonPoint objectAtIndex:i];
CGPoint tempPoint=[tempVal CGPointValue];
NSLog(#"%f,%f",tempPoint.x,tempPoint.y);
}
}
- (void)drawRect:(CGRect)rect
{
[self prepareVertex:5];
for (int i=0; i<5; i++) {
NSValue *tempVal=[polygonPoint objectAtIndex:i];
CGPoint tempPoint=[tempVal CGPointValue];
}
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 5);
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextMoveToPoint(context, [[polygonPoint objectAtIndex:0] CGPointValue].x, [[polygonPoint objectAtIndex:0] CGPointValue].x);
for (int i=1; i<5; i++) {
CGPoint point=[[polygonPoint objectAtIndex:i] CGPointValue];
CGContextAddLineToPoint(context,point.x, point.y);
}
CGContextStrokePath(context);
}
Can anyone tell me whats going on??

Well, the first think to note is that you use black to stroke the polygon, so if your view background is black too, you can't see any thing.
Then, and I think this is the real problem, sin(x) and cos(x) are always between -1 and 1, so the points you generate in:
CGPoint point=CGPointMake(sin((2*M_PI)*i/noOfVertex),(cos(2*M_PI)*i/noOfVertex));
are all located in the rectangle CGrectMake(-1, -1, 2, 2). And that area of your view is very probably hidden by the status bar.
So, if the coordinates you generated are what you are looking for are correct, you may try to remove the status bar or change the coordinates of your view. But I think What you should really do is change the previous line into something like this:
CGFloat x = centerPoint.x + radius * sin(2*M_PI*i/noOfVertex);
CGFloat y = centerPoint.y + radius * cos(2*M_PI*i/noOfVertex);
CGPoint point = CGPointMake(x, y);

It draws something in the upper left corner of the view. The figure is just too small to see easily. That's because the values in polygonPoint (btw you should at least call the array polygonPoints) are between -1.0 and 1.0. You have to translate your points to the center of the view and scale the size accordingly.
Something like
- (void)drawRect:(CGRect)rect {
[self prepareVertex:5];
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 5);
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGFloat x = self.center.x + self.bounds.size.width * 0.5 * [[polygonPoint objectAtIndex:0] CGPointValue].x;
CGFloat y = self.center.y + self.bounds.size.height * 0.5 * [[polygonPoint objectAtIndex:0] CGPointValue].y;
NSLog(#"Point %f/%f", x, y);
CGContextMoveToPoint(context, x, y);
for (int i=1; i<5; i++) {
CGPoint point=[[polygonPoint objectAtIndex:i] CGPointValue];
x = self.center.x + self.bounds.size.width * 0.5 * point.x;
y = self.center.y + self.bounds.size.height * 0.5 * point.y;
NSLog(#"Point %f/%f", x, y);
CGContextAddLineToPoint(context, x, y);
};
CGContextStrokePath(context);
}
does the trick.

Related

Draw line with border iPhone using CGContext

I want to draw line with border,
right now i am doing same thing twice, means first i draw line with stroke +3 and again draw line with normal stroke like 1,
so i do run loop twice for it,
is there any other way for better performance? or any anthor way to draw two line simultaniously.??
My code is
if (locations && ([locations length]/16 > 1)) {
CGPoint point;
polylinewidth=10;
CGContextSetLineWidth(context, polylinewidth);
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
for (int i = 0; i <= locations.length - 32; i += 32) {
NSAutoreleasePool *loc = [[NSAutoreleasePool alloc]init];
CLLocationCoordinate2D coordinates;
coordinates.latitude = hexDecode_iPhone([locations substringWithRange:NSMakeRange(i, 16)]);
coordinates.longitude = hexDecode_iPhone([locations substringWithRange:NSMakeRange(i+16, 16)]);
point = [mapView convertCoordinate:coordinates toPointToView:self.mapView];
if (i == 0)
CGContextMoveToPoint(context, point.x, point.y);
else
CGContextAddLineToPoint(context, point.x, point.y);
[loc drain];
}
CGContextStrokePath(context);
CGContextSetLineWidth(context, polylinewidth-5);
CGContextSetAlpha(context, 0.6);
CGContextSetStrokeColorWithColor(context, color.CGColor);
for (int i = 0; i <= locations.length - 32; i += 32) {
NSAutoreleasePool *loc = [[NSAutoreleasePool alloc]init];
CLLocationCoordinate2D coordinates;
coordinates.latitude = hexDecode_iPhone([locations substringWithRange:NSMakeRange(i, 16)]);
coordinates.longitude = hexDecode_iPhone([locations substringWithRange:NSMakeRange(i+16, 16)]);
point = [mapView convertCoordinate:coordinates toPointToView:self.mapView];
if (i == 0)
CGContextMoveToPoint(context, point.x, point.y);
else
CGContextAddLineToPoint(context, point.x, point.y);
[loc drain];
}
CGContextStrokePath(context);
}
Thanks.

How to Pass Locations From CLLocationManager

How to Pass latitude and Longitude values form CLLocation Manager to My CustomView. I have A ViewController With CLLocationManger where iam getting the Locations, I have Another UIView Class with drawRect Where i have Divided the View with Coordinates as
#define EARTH_RADIUS 6371
- (void)drawRect:(CGRect)rect
{
CGPoint latLong = {41.998035, -116.012215};
CGPoint newCoord = [self convertLatLongCoord:latLong];
NSLog(#"Cartesian Coordinate: (%f, %f)",newCoord.x, newCoord.y);
//Draw dot at coordinate
CGColorRef darkColor = [[UIColor colorWithRed:21.0/255.0
green:92.0/255.0
blue:136.0/255.0
alpha:1.0] CGColor];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, darkColor);
CGContextFillRect(context, CGRectMake(newCoord.x, newCoord.y, 100, 100));
}
-(CGPoint)convertLatLongCoord:(CGPoint)latLong
{
CGFloat x = EARTH_RADIUS * cos(latLong.x) * cos(latLong.y);
CGFloat y = EARTH_RADIUS * cos(latLong.x) * sin(latLong.y);
return CGPointMake(x, y);
}
I have Took the CGPoint latLong = {41.998035, -116.012215} static.
Can you say me How to Pass A ViewController Values to UIView Class
You would generally have a property in you UIView subclass for your coordinate (either using your CGPoint or perhaps a CLLocationCoordinate2D) and then call setNeedsDisplay so that the device knows that it needs to call your drawRect method.
Thus, have a property:
#property (nonatomic) CLLocationCoordinate2D coordinate;
And then update the implementation of your view as such:
- (void)setCoordinate:(CLLocationCoordinate2D)coordinate
{
_coordinate = coordinate;
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
CGPoint newCoord = [self convertLatLongCoord:self.coordinate];
NSLog(#"Cartesian Coordinate: (%f, %f)",newCoord.x, newCoord.y);
//Draw dot at coordinate
CGColorRef darkColor = [[UIColor colorWithRed:21.0/255.0
green:92.0/255.0
blue:136.0/255.0
alpha:1.0] CGColor];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, darkColor);
CGContextSetStrokeColorWithColor(context, darkColor);
CGContextFillRect(context, CGRectMake(newCoord.x, newCoord.y, 100.0, 100.0));
}
// I assume you're using the algorithm from here: https://stackoverflow.com/a/1185413/1271826
- (CGPoint)convertLatLongCoord:(CLLocationCoordinate2D)coordinate
{
CGFloat x = EARTH_RADIUS * cos(coordinate.latitude * M_PI / 180.0) * cos(coordinate.longitude * M_PI / 180.0);
CGFloat y = EARTH_RADIUS * cos(coordinate.latitude * M_PI / 180.0) * sin(coordinate.longitude * M_PI / 180.0);
// TODO: The above calculates x and y values from -EARTH_RADIUS to EARTH_RADIUS.
// You presumably want to scale this appropriately for your view. The
// specifics will vary based upon your desired user interface.
return CGPointMake(x, y);
}
Then, when you want to update your view, you can do something like:
myCustomView.coordinate = CLLocationCoordinate2DMake(41.998035, -116.012215);
While I've tried to remedy some flaws in convertLatLongCoord, it's still not perfect, as it will yield values ranging from -EARTH_RADIUS to EARTH_RADIUS (which obviously won't work for a CGRect of a UIView, which is expecting values between zero and the width/height of the view).
I assume the underlying algorithm is one like discussed here: Converting from longitude\latitude to Cartesian coordinates. I've tried to fix the degrees to radians conversion needed by cos and sin, you still need to scale this and translate it to values appropriate for what portion of the map/radar you're intending to show.
Based on the comment that you want to "find your boat" it sounds like you want the heading from your current location to a specified location (your boat). That calculation is this
+ (float)getHeadingForDirectionFromCoordinate:(CLLocationCoordinate2D)fromLoc toCoordinate:(CLLocationCoordinate2D)toLoc
{
float fLat = degreesToRadians(fromLoc.latitude);
float fLng = degreesToRadians(fromLoc.longitude);
float tLat = degreesToRadians(toLoc.latitude);
float tLng = degreesToRadians(toLoc.longitude);
float degree = radiansToDegrees(atan2(sin(tLng-fLng)*cos(tLat), cos(fLat)*sin(tLat)-sin(fLat)*cos(tLat)*cos(tLng-fLng)));
if (degree >= 0) {
return degree;
} else {
return 360+degree;
}
}

How can I create a part of a filled circle using Core Graphics/Quartz

I have made a subclass of UIView and I am trying to draw part of a circle in my drawRect method.
I have tried using bezierPathWithArcCenter and filling it but that only result in a pie shape (image 3) and that's not what i'm after. I want to draw what you see in image 1 and 2.
Maybe I can clip a full circle somehow? The area around the circle needs to be transparent.
TompaLompas answer pointed me in the right direction (with the arc drawing part). However the complete solution and answer is like this:
#define DEGREES_TO_RADIANS(degrees) ((M_PI * degrees)/ 180)
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
CGContextRef ctx = UIGraphicsGetCurrentContext();
int radius = self.frame.size.width / 2;
CGPoint center = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);
//Image 2
CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);
CGContextAddArc(ctx, center.x, center.y, radius, DEGREES_TO_RADIANS(225), DEGREES_TO_RADIANS(315), NO);
CGContextDrawPath(ctx, kCGPathFill);
}
try overriding drawRect with this:
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
float radius = 50.0f;
float x_left = rect.origin.x;
float x_left_center = x_left + radius;
float y_top = rect.origin.y;
float y_top_center = y_top + radius;
/* Begin path */
CGFloat white[4] = {0.0f, 204.0f/255.0f, 1.0f, 0.8f};
CGContextSetFillColor(context, white);
CGContextSetLineWidth(context, 1.0);
CGContextBeginPath(context);
CGContextMoveToPoint(context, x_left, y_top_center);
CGContextAddArcToPoint(context, x_left, y_top, x_left_center, y_top, radius);
CGContextAddLineToPoint(context,x_left, y_top + radius);
CGContextFillPath(context);
}
It will draw a rotated image number 2

How to create Hexagon in iPhone/iPad

I want to make UIView which is originally rectangular to Hexagon shape to be used in my application.
Please help me something on this.
Thanks in advance.
Subclass UView and override drawRect to draw a hexagon like so:
- (void)drawRect:(CGRect)rect
{
float polySize = 60; // change this
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(160, 90.0);
CGContextMoveToPoint(context, center.x, center.y + polySize);
for(int i = 1; i < 6; ++i)
{
CGFloat x = polySize * sinf(i * 2.0 * M_PI / 6);
CGFloat y = polySize * cosf(i * 2.0 * M_PI / 6);
CGContextAddLineToPoint(context, center.x + x, center.y + y);
}
//Finish Drawing
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathStroke);
CGContextRestoreGState(context);
}
You can create a subclass of UIView and in it's -(void)drawRect: method draw a hexagon. Or Use UIImageView with image of hexagon

CoreGraphics Multi-Colored Line

I have the following code the only seems to use the last colour for the whole line.....
I want the colour to be changing throughout this. Any ideas?
CGContextSetLineWidth(ctx, 1.0);
for(int idx = 0; idx < routeGrabInstance.points.count; idx++)
{
CLLocation* location = [routeGrabInstance.points objectAtIndex:idx];
CGPoint point = [mapView convertCoordinate:location.coordinate toPointToView:self.mapView];
if(idx == 0)
{
// move to the first point
UIColor *tempColor = [self colorForHex:[[routeGrabInstance.pointHeights objectAtIndex:idx] doubleValue]];
CGContextSetStrokeColorWithColor(ctx,tempColor.CGColor);
CGContextMoveToPoint(ctx, point.x, point.y);
}
else
{
UIColor *tempColor = [self colorForHex:[[routeGrabInstance.pointHeights objectAtIndex:idx] doubleValue]];
CGContextSetStrokeColorWithColor(ctx,tempColor.CGColor);
CGContextAddLineToPoint(ctx, point.x, point.y);
}
}
CGContextStrokePath(ctx);
The CGContextSetStrokeColorWithColor only changes the state of the context, it does not do any drawing. The only drawing done in your code is by the CGContextStrokePath at the end. Since each call to CGContextSetStrokeColorWithColor overrides the value set by the previous call the drawing will use the last color set.
You need to create a new path, set the color and then draw in each loop. Something like this:
for(int idx = 0; idx < routeGrabInstance.points.count; idx++)
{
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, x1, y1);
CGContextAddLineToPoint(ctx, x2, y2);
CGContextSetStrokeColorWithColor(ctx,tempColor.CGColor);
CGContextStrokePath(ctx);
}
CGContextSetStrokeColorWithColor sets the stroke color in the context. That color is used when you stroke the path, it has no effect as you continue building the path.
You need to stroke each line separately (CGContextStrokePath).