0

我正在尝试创建一个应用程序,以给定的速度和角度模拟物体在自由落体时的轨迹(更多信息在这里)。GUI 非常简单:2 个数字格式的文本字段(用于速度和角度),1 个显示轨迹的按钮,以及嵌入在滚动视图(ViewController 类)中的自定义视图。我的代码在这里:

视图控制器.h:

#import <Cocoa/Cocoa.h>

@interface ViewController : NSView{
NSBezierPath *path;
NSImage *image;
NSPoint downPoint;
NSPoint currentPoint;
float angle;
float velocity;
}

@property (assign) float angle;
@property (assign) float velocity;
@property (assign) IBOutlet NSTextField *angleField;
@property (assign) IBOutlet NSTextField *speedField;

-(IBAction)fly:(id)sender;
-(NSRect)currentRect; 
@end

视图控制器.m:

#import "ViewController.h"
#import "math.h"

@implementation ViewController
@synthesize angle;
@synthesize velocity;



- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
    path = [[NSBezierPath alloc] init];
    [path setLineWidth:3.0];
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
NSRect bounds = [self bounds];
[[NSColor greenColor] set];
[NSBezierPath fillRect:bounds];
[[NSColor blackColor] set];
[[self pathWithVelocity:velocity angle:angle] stroke];
}

-(IBAction)fly:(id)sender
{
angle = [_angleField floatValue];
velocity = [_speedField floatValue];
NSLog(@"%f - %f",angle,velocity);
[self drawRect:[self bounds]];
}

-(NSBezierPath *)pathWithVelocity:(float)v angle:(float)a{
NSBezierPath* pat = [[NSBezierPath alloc] init];
NSPoint p;
p.x = 0;
p.y = 0;
[pat moveToPoint:p];
for (float t = 0.5; t < 100; t += 0.5) {
    p.x = v*cosf(a)*t;
    p.y = -9.81*t*t/2+v*sinf(a)*t;
    NSLog(@"(%f;%f)",p.x,p.y);
    [pat lineToPoint:p];
}
[pat closePath];
return pat;
}

-(NSRect)currentRect{
float minX = MIN(downPoint.x,currentPoint.x);
float maxX = MAX(downPoint.x,currentPoint.x);
float minY = MIN(downPoint.y,currentPoint.y);
float maxY = MAX(downPoint.x,currentPoint.x);
return NSMakeRect(minX, minY, maxX-minX,maxY-minY);
}
@end

我触发fly方法时出现的问题是:

  • 视图正确变为绿色,但路径未显示(即使点坐标似乎正确)
  • 滚动视图时,新的点坐标显示为速度和角度都为零,即使文本字段的值没有改变

我的代码有什么问题?

编辑:对于角度 0.7 和速度 300,我有这个输出:

2013-03-08 07:14:41.892 Flight[10278:303] (114.73;95.41) 
2013-03-08 07:14:41.893 Flight[10278:303] (229.45;188.36)
2013-03-08 07:14:41.893 Flight[10278:303] (344.18;278.86)
2013-03-08 07:14:41.894 Flight[10278:303] (458.91;366.91)
2013-03-08 07:14:41.894 Flight[10278:303] (573.63;452.51)
2013-03-08 07:14:41.894 Flight[10278:303] (688.36;535.65)
2013-03-08 07:14:41.895 Flight[10278:303] (803.08;616.34)
2013-03-08 07:14:41.895 Flight[10278:303] (917.81;694.58)
2013-03-08 07:14:41.896 Flight[10278:303] (1032.54;770.37)
2013-03-08 07:14:41.896 Flight[10278:303] (1147.26;843.70)
2013-03-08 07:14:41.897 Flight[10278:303] (1261.99;914.58)
2013-03-08 07:14:41.897 Flight[10278:303] (1376.72;983.01)
2013-03-08 07:14:41.897 Flight[10278:303] (1491.44;1048.99)
2013-03-08 07:14:41.898 Flight[10278:303] (1606.17;1112.51)
2013-03-08 07:14:41.898 Flight[10278:303] (1720.90;1173.58)
2013-03-08 07:14:41.898 Flight[10278:303] (1835.62;1232.20)
2013-03-08 07:14:41.899 Flight[10278:303] (1950.35;1288.37)
2013-03-08 07:14:41.899 Flight[10278:303] (2065.07;1342.08)
2013-03-08 07:14:41.899 Flight[10278:303] (2179.80;1393.34)
2013-03-08 07:14:41.900 Flight[10278:303] (2294.53;1442.15)
2013-03-08 07:14:41.900 Flight[10278:303] (2409.25;1488.51)
2013-03-08 07:14:41.900 Flight[10278:303] (2523.98;1532.41)
2013-03-08 07:14:41.901 Flight[10278:303] (2638.71;1573.86)  
2013-03-08 07:14:41.901 Flight[10278:303] (2753.43;1612.86)
2013-03-08 07:14:41.901 Flight[10278:303] (2868.16;1649.41)
2013-03-08 07:14:41.901 Flight[10278:303] (2982.88;1683.50)
2013-03-08 07:14:41.902 Flight[10278:303] (3097.61;1715.15)
2013-03-08 07:14:41.902 Flight[10278:303] (3212.34;1744.33)
2013-03-08 07:14:41.902 Flight[10278:303] (3327.06;1771.07)
2013-03-08 07:14:41.903 Flight[10278:303] (3441.79;1795.35)
2013-03-08 07:14:41.903 Flight[10278:303] (3556.52;1817.19)
2013-03-08 07:14:41.903 Flight[10278:303] (3671.24;1836.56)
2013-03-08 07:14:41.904 Flight[10278:303] (3785.97;1853.49)
2013-03-08 07:14:41.904 Flight[10278:303] (3900.70;1867.97)
2013-03-08 07:14:41.905 Flight[10278:303] (4015.42;1879.99)
2013-03-08 07:14:41.905 Flight[10278:303] (4130.15;1889.56)
2013-03-08 07:14:41.906 Flight[10278:303] (4244.87;1896.67)
2013-03-08 07:14:41.906 Flight[10278:303] (4359.60;1901.34)
2013-03-08 07:14:41.906 Flight[10278:303] (4474.33;1903.55)
2013-03-08 07:14:41.907 Flight[10278:303] (4589.05;1903.31)
2013-03-08 07:14:41.907 Flight[10278:303] (4703.78;1900.61)
2013-03-08 07:14:41.907 Flight[10278:303] (4818.51;1895.47)
2013-03-08 07:14:41.908 Flight[10278:303] (4933.23;1887.87)
2013-03-08 07:14:41.908 Flight[10278:303] (5047.96;1877.82)
2013-03-08 07:14:41.908 Flight[10278:303] (5162.69;1865.31)
2013-03-08 07:14:41.909 Flight[10278:303] (5277.41;1850.36)
2013-03-08 07:14:41.909 Flight[10278:303] (5392.14;1832.95)
2013-03-08 07:14:41.909 Flight[10278:303] (5506.86;1813.09)
2013-03-08 07:14:41.910 Flight[10278:303] (5621.59;1790.77)
2013-03-08 07:14:41.910 Flight[10278:303] (5736.32;1766.01)
2013-03-08 07:14:41.910 Flight[10278:303] (5851.04;1738.79)
2013-03-08 07:14:41.911 Flight[10278:303] (5965.77;1709.12)
2013-03-08 07:14:41.911 Flight[10278:303] (6080.50;1676.99)
2013-03-08 07:14:41.911 Flight[10278:303] (6195.22;1642.42)
2013-03-08 07:14:41.912 Flight[10278:303] (6309.95;1605.39)
2013-03-08 07:14:41.912 Flight[10278:303] (6424.67;1565.91)
2013-03-08 07:14:41.912 Flight[10278:303] (6539.40;1523.97)
2013-03-08 07:14:41.913 Flight[10278:303] (6654.13;1479.59)
2013-03-08 07:14:41.913 Flight[10278:303] (6768.85;1432.75)
2013-03-08 07:14:41.913 Flight[10278:303] (6883.58;1383.46)
2013-03-08 07:14:41.914 Flight[10278:303] (6998.31;1331.72)
2013-03-08 07:14:41.915 Flight[10278:303] (7113.03;1277.52)
2013-03-08 07:14:41.915 Flight[10278:303] (7227.76;1220.87)
2013-03-08 07:14:41.915 Flight[10278:303] (7342.49;1161.77)
2013-03-08 07:14:41.916 Flight[10278:303] (7457.21;1100.22)
2013-03-08 07:14:41.916 Flight[10278:303] (7571.94;1036.21)
2013-03-08 07:14:41.917 Flight[10278:303] (7686.66;969.75)
2013-03-08 07:14:41.917 Flight[10278:303] (7801.39;900.84)
2013-03-08 07:14:41.918 Flight[10278:303] (7916.12;829.48)
2013-03-08 07:14:41.918 Flight[10278:303] (8030.84;755.66)
2013-03-08 07:14:41.919 Flight[10278:303] (8145.57;679.39)
2013-03-08 07:14:41.919 Flight[10278:303] (8260.30;600.67)
2013-03-08 07:14:41.919 Flight[10278:303] (8375.02;519.50)
2013-03-08 07:14:41.920 Flight[10278:303] (8489.75;435.87)
2013-03-08 07:14:41.920 Flight[10278:303] (8604.47;349.79)
2013-03-08 07:14:41.921 Flight[10278:303] (8719.20;261.26)
2013-03-08 07:14:41.921 Flight[10278:303] (8833.93;170.28)
2013-03-08 07:14:41.921 Flight[10278:303] (8948.65;76.84)
2013-03-08 07:14:41.922 Flight[10278:303] (9063.38;-19.05)
2013-03-08 07:14:41.922 Flight[10278:303] (9178.11;-117.39)
2013-03-08 07:14:41.922 Flight[10278:303] (9292.83;-218.18)
2013-03-08 07:14:41.923 Flight[10278:303] (9407.56;-321.43)
2013-03-08 07:14:41.923 Flight[10278:303] (9522.29;-427.13)
2013-03-08 07:14:41.924 Flight[10278:303] (9637.01;-535.28)
2013-03-08 07:14:41.924 Flight[10278:303] (9751.74;-645.88)
2013-03-08 07:14:41.924 Flight[10278:303] (9866.46;-758.94)
2013-03-08 07:14:41.925 Flight[10278:303] (9981.19;-874.45)
2013-03-08 07:14:41.925 Flight[10278:303] (10095.92;-992.41)
2013-03-08 07:14:41.925 Flight[10278:303] (10210.64;-1112.82)
2013-03-08 07:14:41.926 Flight[10278:303] (10325.37;-1235.69)
2013-03-08 07:14:41.926 Flight[10278:303] (10440.10;-1361.00)
2013-03-08 07:14:41.927 Flight[10278:303] (10554.82;-1488.78)
2013-03-08 07:14:41.927 Flight[10278:303] (10669.55;-1619.00)
2013-03-08 07:14:41.927 Flight[10278:303] (10784.28;-1751.68)
2013-03-08 07:14:41.928 Flight[10278:303] (10899.00;-1886.80)
2013-03-08 07:14:41.928 Flight[10278:303] (11013.73;-2024.39)
2013-03-08 07:14:41.929 Flight[10278:303] (11128.45;-2164.42)
2013-03-08 07:14:41.929 Flight[10278:303] (11243.18;-2306.91)
2013-03-08 07:14:41.929 Flight[10278:303] (11357.91;-2451.84)
2013-03-08 07:14:41.930 Flight[10278:303] (11472.63;-2599.23)
2013-03-08 07:14:41.930 Flight[10278:303] (11587.36;-2749.08)
2013-03-08 07:14:41.930 Flight[10278:303] (11702.09;-2901.37)
2013-03-08 07:14:41.931 Flight[10278:303] (11816.81;-3056.12)
2013-03-08 07:14:41.931 Flight[10278:303] (11931.54;-3213.32)
2013-03-08 07:14:41.932 Flight[10278:303] (12046.26;-3372.98)
2013-03-08 07:14:41.932 Flight[10278:303] (12160.99;-3535.08)
2013-03-08 07:14:41.933 Flight[10278:303] (12275.72;-3699.64)
2013-03-08 07:14:41.933 Flight[10278:303] (12390.44;-3866.65)
2013-03-08 07:14:41.934 Flight[10278:303] (12505.17;-4036.12)
2013-03-08 07:14:41.934 Flight[10278:303] (12619.90;-4208.03)
2013-03-08 07:14:41.935 Flight[10278:303] (12734.62;-4382.40)
2013-03-08 07:14:41.935 Flight[10278:303] (12849.35;-4559.22)
2013-03-08 07:14:41.935 Flight[10278:303] (12964.08;-4738.50)
2013-03-08 07:14:41.936 Flight[10278:303] (13078.80;-4920.22)
2013-03-08 07:14:41.936 Flight[10278:303] (13193.53;-5104.40)
2013-03-08 07:14:41.936 Flight[10278:303] (13308.25;-5291.03)
2013-03-08 07:14:41.937 Flight[10278:303] (13422.98;-5480.12)
2013-03-08 07:14:41.937 Flight[10278:303] (13537.71;-5671.65)
2013-03-08 07:14:41.937 Flight[10278:303] (13652.43;-5865.64)
2013-03-08 07:14:41.937 Flight[10278:303] (13767.16;-6062.08)
2013-03-08 07:14:41.938 Flight[10278:303] (13881.89;-6260.98)
2013-03-08 07:14:41.938 Flight[10278:303] (13996.61;-6462.32)
2013-03-08 07:14:41.939 Flight[10278:303] (14111.34;-6666.12)
2013-03-08 07:14:41.939 Flight[10278:303] (14226.07;-6872.37)    
2013-03-08 07:14:41.940 Flight[10278:303] (14340.79;-7081.08)
2013-03-08 07:14:41.940 Flight[10278:303] (14455.52;-7292.23)
2013-03-08 07:14:41.940 Flight[10278:303] (14570.24;-7505.84)
2013-03-08 07:14:41.941 Flight[10278:303] (14684.97;-7721.90)
2013-03-08 07:14:41.941 Flight[10278:303] (14799.70;-7940.41)
4

1 回答 1

0

您获得的坐标的 Y 轴似乎是 -ve。尝试打印坐标并查看您的绘图是否开始。它可能超出了您的视野范围。

于 2013-03-07T21:02:54.297 回答