|
Navigation
Artikel
Stuff
RSS Feeds
|
Sourcecodes - Kreise schneidenSprachenübersicht/C / C++/ C#/Mathematik Keywords: Kreise schneiden, C++, allgemein Kreise schneiden Dieser Artikel soll zeigen, wie man zwei Kreis allgemein schneidet.
Code: 1: (x-x1)^2 + (y-y1)^2 = r1^2 2: (x-x2)^2 + (y-y2)^2 = r2^2
Code: #include <iostream> #include <math.h> struct Point { double x; double y; }; struct Result { struct Point P1; struct Point P2; }; void GetCollisionPoint(struct Point P1, struct Point P2, double r1, double r2, struct Result *res) { double a = (P1.y - P2.y)/(P2.x - P1.x); double b = ( (r1*r1 - r2*r2)- (P1.x*P1.x - P2.x*P2.x) - (P1.y*P1.y - P2.y*P2.y) )/(2*P2.x - P1.x); double e = a*a+1; double f = (2*a*(b-P1.x))-(2*P1.y); double g = (b-P1.x)*(b-P1.x) -r1*r1 + P1.y*P1.y; res->P1.y = (-f + sqrt(f*f - 4*e*g) )/2*e; res->P2.y = (-f - sqrt(f*f - 4*e*g) )/2*e; res->P1.x = res->P1.y * a + b; res->P2.x = res->P2.y * a + b; } int main(int args, char* argv[]) { struct Point A; A.x = -11; A.y = 1; struct Point B; B.x = -1; B.y = 1; struct Result test; GetCollisionPoint(A,B, 1, 1, &test); std::cout << "Position (x,y,z): P(" << test.P1.x << "/" << test.P2.x << "," << test.P1.y << "/" << test.P2.y << ")" << std::endl; return 0; }
Code: debian:/devices/server/projects/SAY# make && ./test g++ -O2 -Wall main.c -o ./test -L /usr/local/lib Position (x,y,z): P(-0/-0,1/1) Gibt es noch irgendwelche Fragen, oder wollen Sie über den Artikel diskutieren? Sprachenübersicht/C / C++/ C#/Mathematik/Kreise schneiden |