0

My current obj-c code:

- (void)slopeMode:(CGPoint)point{
    if (dPoint1.x != -1) //If a coordinate pos has already been set for dPoint1
    {
        dPoint2 = point; //Then set it in dPoint2 instead


        [cPath moveToPoint: CGPointMake(dPoint1.x-5,dPoint1.y-5)];
        [cPath addLineToPoint:CGPointMake(dPoint2.x-5,dPoint2.y-5)];
        [cPath moveToPoint:CGPointMake(dPoint2.x-5,dPoint2.y-5)];

        [cPath addArcWithCenter:CGPointMake(dPoint2.x-5,dPoint2.y-5) radius:10 startAngle:0 endAngle:DEGREES_TO_RADIANS(360) clockwise:YES];

        [cPath closePath];
        [[UIColor greenColor] setStroke];
        [[UIColor greenColor] setFill];

        [cPath fill];
        [cPath stroke];

        gfxP1.path = [cPath CGPath];
        gfxP1.fillColor = [[UIColor greenColor] CGColor];
        gfxP1.lineWidth = 4.0;

        [[wBView layer] addSublayer:gfxP1];

        rSlope = [self calcSlope:dPoint1 :dPoint2];

        [self callAlert:@"Slope Calculation" :[NSString stringWithFormat:@"%.*f",precision,rSlope] :@"OK"];

        //Reset datapoints
        [self resetDPoint:&dPoint1];
        [self resetDPoint:&dPoint2];

        //Reset gfx
        //[self resetCALayer:gfxP1];
        //[self resetCALayer:gfxP2];

        //Reset other vars
        cSlope = -1;
        rSlope = -1;
        tempFStore = @"";
    }

    else
    {
        dPoint1 = point;          

        cPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(dPoint1.x-5,dPoint1.y-5) radius:10 startAngle:0 endAngle:DEGREES_TO_RADIANS(360) clockwise:YES];      
    }
}

My issue is that I am able to display my two points within my UIView object but a line I am trying to add that connects these two points together is not being created or not being displayed correctly. Here is what I am seeing in the IPhone Simulator using the above code:

2 dots enter, no line leaves

Geowil
  • 624
  • 1
  • 12
  • 36
  • refer this link,you may get an idea http://stackoverflow.com/questions/8208469/draw-line-between-two-points-in-iphone and http://stackoverflow.com/questions/31775194/create-line-extensions-for-given-line-created-by-user – Shangari C Apr 30 '16 at 06:08

1 Answers1

0

Try below code(Do modification as per your requirement):

Get two CGPoint and Pass it to method below:

- (void)drawLine:(CGPoint)startPoint endPoint:(CGPoint)endPoint{

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGContextSetStrokeColorWithColor(context, [[UIColor redColor]CGColor]);//adjust color
    CGContextSetLineWidth(context, 1.0);//adjust line width
    CGContextMoveToPoint(context, startPoint.x, startPoint.y);
    CGContextAddLineToPoint(context, endPoint.x, endPoint.y);
    CGContextStrokePath(context);
    CGContextRestoreGState(context);

}
Ronak Chaniyara
  • 5,335
  • 3
  • 24
  • 51