C++ calculate distance between structure of inputed and saved points and the origin, print out list according on distance -
in task simplified version of obstacles shall modeled points detected imaginary sensor system placed in origin of 2 dimensional local cartesian coordinate system. arbitrary many of such obstacles /points in front of vehicle shall possible store in list. list shall sorted euclidian distance origin (see examples below). (hint: write function calculate euclidian distance d of 2 obstacles/points p1 , p2 coordinates (x1,y1) , (x2,y2) formula: d=√(x1−x2)2+(y1−y2)2 identify them each obstacle/point shall store string ("a", "b", ... in example above), distance , coordinates (xi,yi). (hint: give type definition structure these 4 data components/variables of structure.) in loop in function main arbitrary many of such obstacles/points shall possible inputted , stored in list. afterwards sorted distance list shall outputted. beside string, distance , coordinates in each output string of nearest obstacle/point shall computed , outputted additionaly (see example below). (hint: write further function list of obstacles/points first , 1 obstacle/point second parameter calculating neareast other obstacle/point , returning pointer. take care obstacle/point not return , output distance 0 itself, , @ least 2 obstacles/points need exist; otherwise null pointer nullptr shall returned.)
i able create structure function, distance function , list, unfortunately i'm not able sort , use function distance calculate actual distances.
hope can me!
#include<iostream> #include<cmath> using namespace std; double distancecalculate(double x1, double x2, double y1, double y2) { double x = x1-x2; double y = y1-y2; double dist; dist = pow(x, 2) + pow(y , 2); dist = sqrt(dist); return dist; } struct point { char name [1]; double x, y, origin_distance ; struct point *next; }; int main(void) { int choice; struct point *head = nullptr, *newelement, *p ; { cout << "0 end"<< endl; cout <<"1 input point"<< endl; cout <<"2 print list of points" << endl; cin >> choice; switch(choice) { case 1: newelement = new point; if (newelement == nullptr) cerr << "not enough free memory " << endl; else { cout<< "please enter point's name: "; cin>> newelement ->name; cout << "please input value x:"; cin >> newelement->x; cout <<"please input value y:"; cin >> newelement->y; newelement ->next = head; head = newelement; } break; case 2: cout<< "print list of points"<< endl; cout<< "name.\t x.\t y.\t" << endl; p=head; while (p!= nullptr) { cout<< p->name<<"\t"<< p->x<<"\t" << p->y << "\t" << endl; p=p->next; } break; } } while (choice !=0); return 0; }
the first thing notice you're mixing definitions of point , of (linked)list node. more typical implementation see defining 2 types - 1 point , list node. perhaps like:
typedef struct point { double x, y; }; typedef struct listnode { point data; listnode *next; };
however, reading question - doesn't appear defining type capable of representing list part of task. such, may leave std library. i've done that, using std::vector container hold list of points.
not need concern defining datatype hold 2d point, easy sorting free. define function return true or false, depending on whether 1 element should come before next, or not.
there looks nothing wrong distance function, though i'd avoid using pow function when squaring - i'd use x*x + y*y
another advantage of using std::vector
type, can access elements contains using standard array notation. in mind, display nearest , furthest points origin, can sort list display pointlist[0]
, pointlist[numelements-1]
.
here's rough implementation:
#include <cmath> #include <cstdlib> #include <cstdio> #include <vector> #include <algorithm> typedef struct vec2_t { double x; double y; } *pvec2; typedef std::vector<vec2_t> vecvectors; typedef vecvectors::iterator vecvectorsiter; double distbetween(vec2_t &p1, vec2_t &p2) { double dx = p1.x - p2.x, dy = p1.y - p2.y; return sqrt( dx*dx + dy*dy); } bool orderpointasc(vec2_t &p1, vec2_t &p2) { vec2_t origin = {0.0,0.0}; return (distbetween(origin,p1) < distbetween(origin,p2)); } int main() { vecvectors pointlist; vec2_t origin = {0.0, 0.0}; int i, n=10; vec2_t tmp; (i=0; i<n; i++) { tmp.x = 100 * (rand() % 0xffff) / 65535.0; tmp.y = 100 * (rand() % 0xffff) / 65535.0; pointlist.push_back(tmp); printf("%d. - <%5.02f, %5.02f> - dist origin: %0.02f\n", i, tmp.x, tmp.y, distbetween(origin, tmp)); } printf("sorting...\n"); std::sort(pointlist.begin(), pointlist.end(), orderpointasc); (vecvectorsiter iter = pointlist.begin(); iter!=pointlist.end(); iter++) { printf("<%5.02f, %5.02f> - %5.02f\n", (*iter).x, (*iter).y, distbetween(origin, (*iter)) ); } return 0; }
sample output
0. - < 0.06, 28.18> - dist origin: 28.18 1. - < 9.67, 40.44> - dist origin: 41.58 2. - <29.25, 23.99> - dist origin: 37.83 3. - <17.51, 44.80> - dist origin: 48.10 4. - <41.14, 37.33> - dist origin: 55.55 5. - < 8.71, 42.95> - dist origin: 43.82 6. - <35.52, 25.68> - dist origin: 43.83 7. - <15.20, 0.75> - dist origin: 15.22 8. - < 4.57, 18.22> - dist origin: 18.79 9. - < 7.37, 8.29> - dist origin: 11.09 sorting... < 7.37, 8.29> - 11.09 <15.20, 0.75> - 15.22 < 4.57, 18.22> - 18.79 < 0.06, 28.18> - 28.18 <29.25, 23.99> - 37.83 < 9.67, 40.44> - 41.58 < 8.71, 42.95> - 43.82 <35.52, 25.68> - 43.83 <17.51, 44.80> - 48.10 <41.14, 37.33> - 55.55
Comments
Post a Comment