0

UIViewテキストラベルを使用してスパイラルをプロットしようとしています。コードは以下のとおりです。

//
//  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

スクリーンショットは以下のとおりです。 スクリーンショット

なぜこれを行っているのかわかりません。コード全体で float 値を使用しても問題ありませんか? たぶん、私は三角関係で何かを失っています。どんな助けでも大歓迎です。ありがとう!

編集:x、y値を見ました:

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

y 値が正しく計算されていないようです..

固定画像:

修繕!

4

1 に答える 1

2

角度を度数で入力する場合、ラジアンへの適切な変換は次のとおりです。

float radians = degrees * M_PI / 180.0f;

関数への入力も間違っているようです。

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

このコメントは、coils< sides. あなたが入っ105、作っていaroundStep = 2ます。との間0ではありません1

于 2015-01-30T16:06:40.300 に答える