[Code example] struct canonicalize coordinates of rectangle page 131

User avatar

Topic author
BJT
Site Admin
Site Admin
Posts: 141
Joined: Thu Apr 20, 2017 4:43 pm
Instruction: University Student
Gender:
Age: 27
Venezuela

struct canonicalize coordinates of rectangle page 131

Post by BJT » Sat May 13, 2017 10:37 pm

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define min(a, b) ((a) < (b) ? (a) : (b))
  5. #define max(a, b) ((a) > (b) ? (a) : (b))
  6.  
  7. struct point
  8. {
  9.     int x;
  10.     int y;
  11. };
  12.  
  13. struct rect
  14. {
  15.     struct point pt1;
  16.     struct point pt2;
  17. };
  18.  
  19. struct rect canonrect(struct rect r);
  20.  
  21. int main()
  22. {
  23.     struct rect rec1={{200,300},{50,100}},rec2;
  24.     rec2=canonrect(rec1);
  25.     printf("%d %d %d %d\n",rec2.pt1.x,rec2.pt1.y,rec2.pt2.x,rec2.pt2.y);
  26.     return EXIT_SUCCESS;
  27. }
  28.  
  29. /* canonrect: canonicalize coordinates of rectangle */
  30. struct rect canonrect(struct rect r)
  31. {
  32.     struct rect temp;
  33.     temp.pt1.x = min(r.pt1.x,r.pt2.x);
  34.     temp.pt1.y = min(r.pt1.y,r.pt2.y);
  35.     temp.pt2.x = max(r.pt1.x,r.pt2.x);
  36.     temp.pt2.y = max(r.pt1.y,r.pt2.y);
  37.     return temp;
  38. }

ouput

Code: Select all

50,100,200,300
word count: 148

Link:
BBcode:
HTML:
Hide post links
Show post links

Who is online

Users browsing this forum: No registered users and 1 guest