Skip to content

Instantly share code, notes, and snippets.

@moray95
Created March 7, 2013 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moray95/5108918 to your computer and use it in GitHub Desktop.
Save moray95/5108918 to your computer and use it in GitHub Desktop.
Modelling free fall of an object
#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
#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
@thebsdbox
Copy link

Don't call -drawRect: directly -setNeedsDisplay:YES is what should be called..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment