Learning by doing: Reading books and trying to understand the (code) examples
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
578 B

  1. #include <stdio.h>
  2. #include <math.h>
  3. typedef struct point {
  4. double x, y;
  5. } point;
  6. typedef struct {
  7. union {
  8. struct point;
  9. point p2;
  10. };
  11. double z;
  12. } threepoint;
  13. double length(point p) {
  14. return sqrt(p.x * p.x + p.y * p.y);
  15. }
  16. double threelength(threepoint p) {
  17. return sqrt(p.x * p.x + p.y * p.y + p.z * p.z);
  18. }
  19. int main() {
  20. threepoint p = {.x = 3, .y = 0, .z = 4};
  21. printf("p is %g units from the origin\n", threelength(p));
  22. double xylength = length(p.p2);
  23. printf("Its projection onto the XY plane is %g units from the origin\n", xylength);
  24. }