Search code examples
iosuiviewspiral

Trying to plot a spiral, just draws a line


I am trying to plot a spiral in a UIView just using text labels. Code is below:

//
//  ViewController.m
//  spiral
//
//  Created on 30/01/2015.
//  Copyright (c) 2015  All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (strong, nonatomic) IBOutlet UIView *canvas;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //[self plotPoints:self.view.frame.size.width/2 andY:self.view.frame.size.height/2];

    // centerX-- X origin of the spiral.
    // centerY-- Y origin of the spiral.
    // radius--- Distance from origin to outer arm.
    // sides---- Number of points or sides along the spiral's arm.
    // coils---- Number of coils or full rotations. (Positive numbers spin clockwise, negative numbers spin counter-clockwise)
    // rotation- Overall rotation of the spiral. ('0'=no rotation, '1'=360 degrees, '180/360'=180 degrees)
    //

    [self spiral:170 andY:284 andRadius:100 withSides:20 andCoils:10 andRotation:0];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void) spiral: (float)centerX andY:(float)centerY andRadius:(float)radius withSides: (float)sides andCoils:(float)coils andRotation:(float)rotation
{
    //
    // How far to step away from center for each side.
    float awayStep = radius/sides;
    //
    // How far to rotate around center for each side.
    float aroundStep = coils/sides;// 0 to 1 based.
    //
    // Convert aroundStep to radians.
    float aroundRadians = aroundStep * 2 * M_PI;
    //
    // Convert rotation to radians.
    rotation *= 2 * M_PI;
    //
    // For every side, step around and away from center.
    for(int i=1; i<=sides; i++){

        //
        // How far away from center
        float away = i * awayStep;
        //
        // How far around the center.
        float around = i * aroundRadians + rotation;
        //
        // Convert 'around' and 'away' to X and Y.
        float x = centerX + cos(around) * away;
        float y = centerY + sin(around) * away;
        //


        [self plotPoints:x andY:y];
    }
}


-(void)plotPoints: (float)x andY:(float) y {

    UILabel *point = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 22, 22)];

    point.font= [UIFont systemFontOfSize:19];

    point.text=@"*";
    point.center = CGPointMake(x, y);
    [self.canvas addSubview:point];
    point.hidden=NO;

}

@end

screenshot is below: screenshot

Not sure why it's doing this. Is it okay to use float values all the way through the code? Maybe I'm losing something in the trig. Any help would be appreciated. Thanks!

edit: looked at the x,y values:

2015-01-31 00:02:32.073 spiral[86741:6997860] x: 165.000000, y:284.000000
2015-01-31 00:02:32.075 spiral[86741:6997860] x: 180.000000, y:284.000000
2015-01-31 00:02:32.076 spiral[86741:6997860] x: 155.000000, y:284.000000
2015-01-31 00:02:32.076 spiral[86741:6997860] x: 190.000000, y:284.000000
2015-01-31 00:02:32.077 spiral[86741:6997860] x: 145.000000, y:283.999969
2015-01-31 00:02:32.077 spiral[86741:6997860] x: 200.000000, y:284.000000
2015-01-31 00:02:32.078 spiral[86741:6997860] x: 135.000000, y:283.999939
2015-01-31 00:02:32.078 spiral[86741:6997860] x: 210.000000, y:284.000031
2015-01-31 00:02:32.079 spiral[86741:6997860] x: 125.000000, y:284.000000
2015-01-31 00:02:32.079 spiral[86741:6997860] x: 220.000000, y:284.000061
2015-01-31 00:02:32.080 spiral[86741:6997860] x: 115.000000, y:283.999847
2015-01-31 00:02:32.080 spiral[86741:6997860] x: 230.000000, y:284.000000
2015-01-31 00:02:32.081 spiral[86741:6997860] x: 105.000000, y:283.999908
2015-01-31 00:02:32.081 spiral[86741:6997860] x: 240.000000, y:284.000183
2015-01-31 00:02:32.081 spiral[86741:6997860] x: 95.000000, y:284.000000
2015-01-31 00:02:32.082 spiral[86741:6997860] x: 250.000000, y:284.000122
2015-01-31 00:02:32.160 spiral[86741:6997860] x: 85.000000, y:283.999786
2015-01-31 00:02:32.160 spiral[86741:6997860] x: 260.000000, y:284.000000
2015-01-31 00:02:32.161 spiral[86741:6997860] x: 75.000000, y:283.999878
2015-01-31 00:02:32.161 spiral[86741:6997860] x: 270.000000, y:284.000275

seems like the y value isn't computing correctly..

fixed image:

fixed!


Solution

  • If you are entering your angles in degress, the proper conversion to radians is:

    float radians = degrees * M_PI / 180.0f;
    

    Your input to the function appears to be wrong, too.

    float aroundStep = coils/sides;// 0 to 1 based.
    

    This comment suggests that coils < sides. You have entered 10 and 5, making aroundStep = 2; not between 0 and 1.