Code
// // Oct24 // #include <iostream> #include <cmath> // #include "math.h" using namespace std; struct Point { double x, y; }; struct Size { double width, height; }; struct Triangle { Point a, b, c; }; struct Rectangle { Point topLeft; Size widthHeight; }; struct Circle { double radius; Point center; }; struct Ellipse { Point f1, f2; double foo; }; struct Rotation { double angle; Point center; }; struct Ellipse2 { Point center; double radiusX; double radiusY; Rotation rotation; }; struct Polygon { int numberOfPoints; Point p1, p2, p3, p4, p5, p6, p7, p8; }; struct ResistorColor { std::string color; int value; }; double dist (Point a, Point b) { // Pythagoream Theorem a^2 + b^2 = c^2 double dx = a.x - b.x; double dy = a.y - b.y; return (sqrt (dx * dx + dy * dy)); } double triangleArea (Triangle triangle) { // Compute the area of a triangle from its corner points // 1. Compute the lengths of the side double a = dist(triangle.b, triangle.c); double b = dist(triangle.a, triangle.c); double c = dist(triangle.a, triangle.b); // 2. Plug the lengths into the standard formula double s = (a + b + c) / 2; double t = s * (s-a) * (s-b) * (s-c); return sqrt (t); // <cmath> } string drawTriangle (Triangle t, string fill, string stroke) { return "<polygon points='" + to_string(t.a.x) + " " + to_string(t.a.y) + " " + to_string(t.b.x) + " " + to_string(t.b.x) + " " + to_string(t.c.x) + " " + to_string(t.c.y) + " z' " + "fill='" + fill + "' stroke='" + stroke + "' />"; } int main(int argc, const char * argv[]) { Triangle t = { { -3, -4 }, { 6, 0 }, { 0, 8 } }; cout << triangleArea (t) << endl; cout << drawTriangle (t, "red", "blue") << endl; return 0; }
No comments:
Post a Comment