Created
March 7, 2013 15:40
-
-
Save moray95/5108918 to your computer and use it in GitHub Desktop.
Modelling free fall of an object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Don't call -drawRect: directly -setNeedsDisplay:YES is what should be called..