计算几何入门:向量运算与距离计算 - C++ 代码填空题
#include<bits/stdc++.h> using namespace std; const int N = 205; int n, m; struct Point{ int x, y; double det(){ return sqrt((double)x * x + y * y); } Point operator+(const Point& p){ Point res; res.x = x + p.x; res.y = y + p.y; return res; } } p[N]; Point operator-(const Point& a, const Point& b){ Point res; res.x = a.x - b.x; res.y = a.y - b.y; return res; } int dot(const Point& a, const Point& b){ return a.x * b.x + a.y * b.y; } int cross(const Point& a, const Point& b){ return a.x * b.y - a.y * b.x; } double dis(const Point& a, const Point& b){ int dx = a.x - b.x; int dy = a.y - b.y; return sqrt(dx * dx + dy * dy); } int main() { scanf("%d%d", &n, &m); for(int i = 1; i <= n; ++i) scanf("%d%d", &p[i].x, &p[i].y); for(int i = 1; i <= m; ++i){ int op, r, s; scanf("%d%d", &op, &r); if(op != 1) scanf("%d", &s); if(op == 1){ printf(".2lf\n", p[r].det()); } else if(op == 2){ Point t = p[r] + p[s]; printf("%d %d\n", t.x, t.y); } else if(op == 3){ Point t = p[r] - p[s]; printf("%d %d\n", t.x, t.y); } else if(op == 4){ printf("%d\n", dot(p[r], p[s])); } else if(op == 5){ printf("%d\n", cross(p[r], p[s])); } else if(op == 6){ printf(".2lf\n", dis(p[r], p[s])); } }
return 0;
}
原文地址: http://www.cveoy.top/t/topic/fPpd 著作权归作者所有。请勿转载和采集!