Marco Oehler
/
Lab5
ROME 2 Lab5
Point.cpp@1:5201940a41c1, 2020-06-12 (annotated)
- Committer:
- oehlemar
- Date:
- Fri Jun 12 08:19:42 2020 +0000
- Revision:
- 1:5201940a41c1
- Parent:
- 0:893a1e710078
asdf
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
oehlemar | 0:893a1e710078 | 1 | /* |
oehlemar | 0:893a1e710078 | 2 | * Point.cpp |
oehlemar | 0:893a1e710078 | 3 | * Copyright (c) 2020, ZHAW |
oehlemar | 0:893a1e710078 | 4 | * All rights reserved. |
oehlemar | 0:893a1e710078 | 5 | */ |
oehlemar | 0:893a1e710078 | 6 | |
oehlemar | 0:893a1e710078 | 7 | #include <cmath> |
oehlemar | 0:893a1e710078 | 8 | #include "Point.h" |
oehlemar | 0:893a1e710078 | 9 | |
oehlemar | 0:893a1e710078 | 10 | using namespace std; |
oehlemar | 0:893a1e710078 | 11 | |
oehlemar | 0:893a1e710078 | 12 | /** |
oehlemar | 0:893a1e710078 | 13 | * Creates a Point object. |
oehlemar | 0:893a1e710078 | 14 | */ |
oehlemar | 0:893a1e710078 | 15 | Point::Point() { |
oehlemar | 0:893a1e710078 | 16 | |
oehlemar | 0:893a1e710078 | 17 | x = 0.0f; |
oehlemar | 0:893a1e710078 | 18 | y = 0.0f; |
oehlemar | 0:893a1e710078 | 19 | r = 0.0f; |
oehlemar | 0:893a1e710078 | 20 | alpha = 0.0f; |
oehlemar | 0:893a1e710078 | 21 | } |
oehlemar | 0:893a1e710078 | 22 | |
oehlemar | 0:893a1e710078 | 23 | /** |
oehlemar | 0:893a1e710078 | 24 | * Creates a Point object from given polar coordinates. |
oehlemar | 0:893a1e710078 | 25 | * @param r the radius of a point. |
oehlemar | 0:893a1e710078 | 26 | * @param alpha the angle of a point. |
oehlemar | 0:893a1e710078 | 27 | */ |
oehlemar | 0:893a1e710078 | 28 | Point::Point(float r, float alpha) { |
oehlemar | 0:893a1e710078 | 29 | |
oehlemar | 0:893a1e710078 | 30 | x = r*cos(alpha); |
oehlemar | 0:893a1e710078 | 31 | y = r*sin(alpha); |
oehlemar | 0:893a1e710078 | 32 | |
oehlemar | 0:893a1e710078 | 33 | this->r = r; |
oehlemar | 0:893a1e710078 | 34 | this->alpha = alpha; |
oehlemar | 0:893a1e710078 | 35 | } |
oehlemar | 0:893a1e710078 | 36 | |
oehlemar | 0:893a1e710078 | 37 | /** |
oehlemar | 0:893a1e710078 | 38 | * Deletes this object. |
oehlemar | 0:893a1e710078 | 39 | */ |
oehlemar | 0:893a1e710078 | 40 | Point::~Point() {} |
oehlemar | 0:893a1e710078 | 41 | |
oehlemar | 0:893a1e710078 | 42 | /** |
oehlemar | 0:893a1e710078 | 43 | * Calculates the distance of this point from the origin. |
oehlemar | 0:893a1e710078 | 44 | */ |
oehlemar | 0:893a1e710078 | 45 | float Point::distance() { |
oehlemar | 0:893a1e710078 | 46 | |
oehlemar | 0:893a1e710078 | 47 | return sqrt(x*x+y*y); |
oehlemar | 0:893a1e710078 | 48 | } |
oehlemar | 0:893a1e710078 | 49 | |
oehlemar | 0:893a1e710078 | 50 | /** |
oehlemar | 0:893a1e710078 | 51 | * Calculates the distance between this point and a given point. |
oehlemar | 0:893a1e710078 | 52 | * @param point another point to calculate the distance to. |
oehlemar | 0:893a1e710078 | 53 | */ |
oehlemar | 0:893a1e710078 | 54 | float Point::distance(Point& point) { |
oehlemar | 0:893a1e710078 | 55 | |
oehlemar | 0:893a1e710078 | 56 | return sqrt((x-point.x)*(x-point.x)+(y-point.y)*(y-point.y)); |
oehlemar | 0:893a1e710078 | 57 | } |
oehlemar | 0:893a1e710078 | 58 | |
oehlemar | 0:893a1e710078 | 59 | /** |
oehlemar | 0:893a1e710078 | 60 | * Calculates the manhattan distance of this point from the origin. |
oehlemar | 0:893a1e710078 | 61 | * This calculation is only an approximation, but a lot faster to compute. |
oehlemar | 0:893a1e710078 | 62 | */ |
oehlemar | 0:893a1e710078 | 63 | float Point::manhattanDistance() { |
oehlemar | 0:893a1e710078 | 64 | |
oehlemar | 0:893a1e710078 | 65 | return fabs(x)+fabs(y); |
oehlemar | 0:893a1e710078 | 66 | } |
oehlemar | 0:893a1e710078 | 67 | |
oehlemar | 0:893a1e710078 | 68 | /** |
oehlemar | 0:893a1e710078 | 69 | * Calculates the manhattan distance between this point and a given point. |
oehlemar | 0:893a1e710078 | 70 | * This calculation is only an approximation, but a lot faster to compute. |
oehlemar | 0:893a1e710078 | 71 | * @param point another point to calculate the distance to. |
oehlemar | 0:893a1e710078 | 72 | */ |
oehlemar | 0:893a1e710078 | 73 | float Point::manhattanDistance(Point& point) { |
oehlemar | 0:893a1e710078 | 74 | |
oehlemar | 0:893a1e710078 | 75 | return fabs(x-point.x)+fabs(y-point.y); |
oehlemar | 0:893a1e710078 | 76 | } |
oehlemar | 0:893a1e710078 | 77 |