Created
October 9, 2024 20:09
-
-
Save pgtwitter/5cd1790e99c76458c89ff96ff2a31d86 to your computer and use it in GitHub Desktop.
Circumscribed Circle
This file contains hidden or 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 MyView : NSView | |
@end | |
@implementation MyView | |
double len(NSPoint p){return sqrt(p.x*p.x+p.y*p.y);} | |
NSRect bboxOfCircumscribedCircle(NSPoint p0, NSPoint p1, NSPoint p2) { | |
double a= len(NSMakePoint(p2.x-p1.x, p2.y-p1.y)); | |
double b= len(NSMakePoint(p2.x-p0.x, p2.y-p0.y)); | |
double c= len(NSMakePoint(p1.x-p0.x, p1.y-p0.y)); | |
double Ca=a*a*(b*b+c*c-a*a); | |
double Cb=b*b*(c*c+a*a-b*b); | |
double Cc=c*c*(a*a+b*b-c*c); | |
double tmp= Ca+Cb+Cc; | |
if (tmp<=0.0) return NSZeroRect; | |
double S= 1.0/4.0*sqrt(tmp); | |
double R= a*b*c/4/S; | |
double CS= 1.0/16.0/S/S; | |
NSPoint O= NSMakePoint(CS*(Ca*p0.x+Cb*p1.x+Cc*p2.x),CS*(Ca*p0.y+Cb*p1.y+Cc*p2.y)); | |
return NSMakeRect(O.x-R, O.y-R, 2*R, 2*R); | |
} | |
NSRect handle(NSPoint p){return NSMakeRect(p.x-2, p.y-2, 4, 4);} | |
NSPoint midPoint(NSRect r){return NSMakePoint(NSMidX(r), NSMidY(r));} | |
- (void)drawRect:(NSRect)dirtyRect { | |
[[NSColor whiteColor] set]; | |
NSRectFill(dirtyRect); | |
for(NSUInteger x=50;x<200;x+=25) { | |
NSPoint p0= NSMakePoint(100, 300); | |
NSPoint p1= NSMakePoint(x, x); | |
NSPoint p2= NSMakePoint(300, 100); | |
[[NSColor blackColor] set]; | |
NSFrameRect(handle(p0)); | |
NSFrameRect(handle(p1)); | |
NSFrameRect(handle(p2)); | |
NSRect rect= bboxOfCircumscribedCircle(p0, p1, p2); | |
NSFrameRect(rect); | |
[[NSColor redColor] set]; | |
NSRectFill(handle(midPoint(rect))); | |
[[NSBezierPath bezierPathWithOvalInRect:rect] stroke]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment