Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
EuclidPoint.cpp@5:d7d16cb9c974, 2015-08-17 (annotated)
- Committer:
- tyleralt
- Date:
- Mon Aug 17 21:56:25 2015 +0000
- Revision:
- 5:d7d16cb9c974
- Parent:
- 1:bb1507f0bb64
export commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tyleralt | 0:ded79d89abdf | 1 | //#include "Point.h" |
tyleralt | 0:ded79d89abdf | 2 | #include "EuclidPoint.h" |
tyleralt | 0:ded79d89abdf | 3 | #include <math.h> |
tyleralt | 0:ded79d89abdf | 4 | |
tyleralt | 1:bb1507f0bb64 | 5 | Point EuclidPoint :: getPoint(){ |
tyleralt | 1:bb1507f0bb64 | 6 | int degree = (atan2((double)y, (double)x)) * 57.295; |
tyleralt | 1:bb1507f0bb64 | 7 | if (degree < 0){ |
tyleralt | 1:bb1507f0bb64 | 8 | degree = 360 + degree; |
tyleralt | 1:bb1507f0bb64 | 9 | } |
tyleralt | 1:bb1507f0bb64 | 10 | double dradius = sqrt((double)(x * x ) + (double)(y * y)); |
tyleralt | 5:d7d16cb9c974 | 11 | int radius = rint(dradius) - 2; |
tyleralt | 1:bb1507f0bb64 | 12 | Point pointer = Point(degree, z, radius); |
tyleralt | 1:bb1507f0bb64 | 13 | return pointer; |
tyleralt | 1:bb1507f0bb64 | 14 | } |
tyleralt | 1:bb1507f0bb64 | 15 | |
tyleralt | 0:ded79d89abdf | 16 | int EuclidPoint:: getFirstCrossingDegree (int xl, int yl){ // returns the degree of the first cross |
tyleralt | 0:ded79d89abdf | 17 | |
tyleralt | 0:ded79d89abdf | 18 | double dradius = sqrt((double)(xl * xl ) + (yl * yl)); |
tyleralt | 0:ded79d89abdf | 19 | int radius = rint(dradius); |
tyleralt | 0:ded79d89abdf | 20 | |
tyleralt | 0:ded79d89abdf | 21 | int oneCross = (int)(asin ((double)yl / radius)) * 57.295; |
tyleralt | 0:ded79d89abdf | 22 | if (xl < 0 && oneCross > 0){ |
tyleralt | 0:ded79d89abdf | 23 | oneCross = 180 - oneCross; |
tyleralt | 0:ded79d89abdf | 24 | } else if (xl < 0 && oneCross < 0){ |
tyleralt | 0:ded79d89abdf | 25 | oneCross = 180 - oneCross; |
tyleralt | 0:ded79d89abdf | 26 | } else if (xl < 0){ |
tyleralt | 0:ded79d89abdf | 27 | oneCross = 360 + oneCross; |
tyleralt | 0:ded79d89abdf | 28 | } |
tyleralt | 0:ded79d89abdf | 29 | |
tyleralt | 0:ded79d89abdf | 30 | int twoCross = (int) (acos ((double)xl / radius)) * 57.295; |
tyleralt | 0:ded79d89abdf | 31 | if (yl < 0){ |
tyleralt | 0:ded79d89abdf | 32 | twoCross = 360 - twoCross; |
tyleralt | 0:ded79d89abdf | 33 | } |
tyleralt | 0:ded79d89abdf | 34 | if (oneCross < twoCross){ |
tyleralt | 0:ded79d89abdf | 35 | return twoCross; |
tyleralt | 0:ded79d89abdf | 36 | } else { |
tyleralt | 0:ded79d89abdf | 37 | return oneCross; |
tyleralt | 0:ded79d89abdf | 38 | } |
tyleralt | 0:ded79d89abdf | 39 | return 1; |
tyleralt | 0:ded79d89abdf | 40 | } |
tyleralt | 0:ded79d89abdf | 41 | |
tyleralt | 0:ded79d89abdf | 42 | int EuclidPoint :: getSecondCrossingDegree (int xl, int yl){ // returns the degree of the first cross |
tyleralt | 0:ded79d89abdf | 43 | double dradius = (sqrt((double)(xl * xl ) + (yl * yl))); |
tyleralt | 0:ded79d89abdf | 44 | int radius = rint(dradius); |
tyleralt | 0:ded79d89abdf | 45 | |
tyleralt | 0:ded79d89abdf | 46 | int oneCross = (int)(asin ((double)yl / radius)) * 57.295; |
tyleralt | 0:ded79d89abdf | 47 | if (xl < 0 && oneCross > 0){ |
tyleralt | 0:ded79d89abdf | 48 | oneCross = 180 - oneCross; |
tyleralt | 0:ded79d89abdf | 49 | } else if (xl < 0 && oneCross < 0){ |
tyleralt | 0:ded79d89abdf | 50 | oneCross = 180 - oneCross; |
tyleralt | 0:ded79d89abdf | 51 | } else if (xl < 0){ |
tyleralt | 0:ded79d89abdf | 52 | oneCross = 360 + oneCross; |
tyleralt | 0:ded79d89abdf | 53 | } |
tyleralt | 0:ded79d89abdf | 54 | int twoCross = (int) (acos ((double)xl / radius)) * 57.295; |
tyleralt | 0:ded79d89abdf | 55 | if (yl < 0){ |
tyleralt | 0:ded79d89abdf | 56 | twoCross = 360 - twoCross; |
tyleralt | 0:ded79d89abdf | 57 | } |
tyleralt | 0:ded79d89abdf | 58 | |
tyleralt | 0:ded79d89abdf | 59 | if (oneCross < twoCross){ |
tyleralt | 0:ded79d89abdf | 60 | return oneCross; |
tyleralt | 0:ded79d89abdf | 61 | } else { |
tyleralt | 0:ded79d89abdf | 62 | return twoCross; |
tyleralt | 0:ded79d89abdf | 63 | } |
tyleralt | 0:ded79d89abdf | 64 | return 1; |
tyleralt | 0:ded79d89abdf | 65 | } |
tyleralt | 0:ded79d89abdf | 66 | |
tyleralt | 0:ded79d89abdf | 67 | |
tyleralt | 1:bb1507f0bb64 | 68 | |
tyleralt | 0:ded79d89abdf | 69 | EuclidPoint :: EuclidPoint(void){ |
tyleralt | 0:ded79d89abdf | 70 | x = 0; |
tyleralt | 0:ded79d89abdf | 71 | y = 3; |
tyleralt | 0:ded79d89abdf | 72 | z = 4; |
tyleralt | 0:ded79d89abdf | 73 | } |
tyleralt | 0:ded79d89abdf | 74 | EuclidPoint :: EuclidPoint(int xi, int yi, int zi){ |
tyleralt | 0:ded79d89abdf | 75 | x = xi; |
tyleralt | 0:ded79d89abdf | 76 | y = yi; |
tyleralt | 0:ded79d89abdf | 77 | z = zi; |
tyleralt | 0:ded79d89abdf | 78 | } |
tyleralt | 0:ded79d89abdf | 79 | |
tyleralt | 0:ded79d89abdf | 80 | Point EuclidPoint :: getStartPoint(void){ |
tyleralt | 0:ded79d89abdf | 81 | int degree; |
tyleralt | 0:ded79d89abdf | 82 | if (x >= 0 && y >= 0){ //1 |
tyleralt | 0:ded79d89abdf | 83 | degree = getFirstCrossingDegree(x + 1, y); |
tyleralt | 0:ded79d89abdf | 84 | } else if (x < 0 && y >= 0){ //2 |
tyleralt | 0:ded79d89abdf | 85 | degree = getFirstCrossingDegree(x + 1, y + 1); |
tyleralt | 0:ded79d89abdf | 86 | }else if (x < 0 && y < 0){ //3 |
tyleralt | 0:ded79d89abdf | 87 | degree = getFirstCrossingDegree(x,y+1); |
tyleralt | 0:ded79d89abdf | 88 | }else{ //4 |
tyleralt | 0:ded79d89abdf | 89 | degree = getFirstCrossingDegree(x,y); |
tyleralt | 0:ded79d89abdf | 90 | } |
tyleralt | 0:ded79d89abdf | 91 | int radius = (int) sqrt((double)(x * x ) + (y * y)); |
tyleralt | 0:ded79d89abdf | 92 | return Point( degree, z, radius); |
tyleralt | 0:ded79d89abdf | 93 | } |
tyleralt | 0:ded79d89abdf | 94 | Point EuclidPoint :: getEndPoint(void){ |
tyleralt | 0:ded79d89abdf | 95 | int degree; |
tyleralt | 0:ded79d89abdf | 96 | if (x >= 0 && y >= 0){ //1 |
tyleralt | 0:ded79d89abdf | 97 | degree = getFirstCrossingDegree(x, y + 1); |
tyleralt | 0:ded79d89abdf | 98 | } else if (x < 0 && y >= 0){ //2 |
tyleralt | 0:ded79d89abdf | 99 | degree = getFirstCrossingDegree(x , y ); |
tyleralt | 0:ded79d89abdf | 100 | }else if (x < 0 && y < 0){ //3 |
tyleralt | 0:ded79d89abdf | 101 | degree = getFirstCrossingDegree(x + 1,y); |
tyleralt | 0:ded79d89abdf | 102 | }else{ //4 |
tyleralt | 0:ded79d89abdf | 103 | degree = getFirstCrossingDegree(x+ 1,y + 1); |
tyleralt | 0:ded79d89abdf | 104 | } |
tyleralt | 0:ded79d89abdf | 105 | int radius = (int) sqrt((double)(x * x ) + (y * y)); |
tyleralt | 0:ded79d89abdf | 106 | return Point( degree, z, radius); |
tyleralt | 0:ded79d89abdf | 107 | } |
tyleralt | 0:ded79d89abdf | 108 | void EuclidPoint :: moveUp(void){ |
tyleralt | 0:ded79d89abdf | 109 | z++; |
tyleralt | 0:ded79d89abdf | 110 | } |
tyleralt | 0:ded79d89abdf | 111 | void EuclidPoint :: moveDown(void){ |
tyleralt | 0:ded79d89abdf | 112 | z--; |
tyleralt | 0:ded79d89abdf | 113 | } |
tyleralt | 0:ded79d89abdf | 114 | void EuclidPoint :: moveTowards(void){ |
tyleralt | 0:ded79d89abdf | 115 | y--; |
tyleralt | 0:ded79d89abdf | 116 | } |
tyleralt | 0:ded79d89abdf | 117 | void EuclidPoint :: moveAway(void){ |
tyleralt | 0:ded79d89abdf | 118 | y++; |
tyleralt | 0:ded79d89abdf | 119 | } |
tyleralt | 0:ded79d89abdf | 120 | void EuclidPoint :: moveRight(void){ |
tyleralt | 0:ded79d89abdf | 121 | x++; |
tyleralt | 0:ded79d89abdf | 122 | } |
tyleralt | 0:ded79d89abdf | 123 | void EuclidPoint :: moveLeft(void){ |
tyleralt | 0:ded79d89abdf | 124 | x--; |
tyleralt | 0:ded79d89abdf | 125 | } |