Code Tidy - Pastebin

New     Fork     Embed     View raw     Report
Tri.c - c

Embed

You can embed this paste into a blog or website with this code:

<iframe class="codetidy" type="text/html" width="100%" src="http://codetidy.com/paste/embed/171" frameborder="0"></iframe>

Add comment

Captcha
  1. typedef struct {?
  2.     float x;?
  3.     float y;?
  4.     float z;?
  5. } Vector3;?
  6.  ?
  7. typedef struct {?
  8.     Vector3 a;?
  9.     Vector3 b;?
  10.     Vector3 c;?
  11. } Triangle;?
  12.  ?
  13. typedef struct {?
  14.     Vector3 p;?
  15.     Vector3 normal;?
  16. } Plane;?
  17.  ?
  18. // Dot product of two vectors?
  19. float dot (Vector3 *v1, Vector3 *v2) {?
  20.     return v1->x * v2->x + v1->y * v2->y + v1->z * v2->z; ?
  21. }?
  22.  ?
  23. // Magnitude of a vector?
  24. float vectorMagnitude(Vector3 *v) {?
  25.     return sqrt(v->x * v->x + v->y * v->y + v->z * v->z);?
  26. }?
  27.  ?
  28. // Normalized version of a vector?
  29. Vector3 normalizedVector(Vector3 *v) {?
  30.     float mag = vectorMagnitude(v);?
  31.     Vector3 result = { v->x/mag, v->y/mag, v->z/mag };?
  32.     return result;?
  33. }?
  34.  ?
  35. // Multiply a vector by a float?
  36. Vector3 vectorMult(Vector3 *v, float f) {?
  37.     Vector3 result = { v->x*f, v->y*f, v->z*f };?
  38.     return result;?
  39. }?
  40.  ?
  41. // Subtract v2 from v1?
  42. Vector3 vectorSubtr(Vector3 *v1, Vector3 *v2) {?
  43.     Vector3 result = { v1->x-v2->x, v1->y-v2->y, v1->z-v2->z };?
  44.     return result;?
  45. }?
  46.  ?
  47. // Add a vector to another vector?
  48. Vector3 vectorAdd(Vector3 *v, Vector3 *p) {?
  49.     Vector3 result = { v->x+p->x, v->y+p->y, v->z+p->z };?
  50.     return result;?
  51. }?
  52.  ?
  53. // Finds the point on a plane nearest the origin?
  54. Vector3 pointNearestOrigin (Plane *plane) {?
  55.     float d = dot(&(plane->normal), &(plane->p));?
  56.     Vector3 norm = normalizedVector(&(plane->normal));?
  57.     Vector3 result = vectorMult(&norm, d);?
  58.     return result;?
  59. }?
  60.  ?
  61. // Project a vector in space onto a plane that intersects the origin.?
  62. Vector3 projectPointOnPlane(Vector3 *point, Plane *plane) {?
  63.     Vector3 n = normalizedVector(&(plane->normal));?
  64.     Vector3 u = vectorMult(&n, dot(point, &n));?
  65.     Vector3 projected = vectorSubtr(point, &u);?
  66.     projected = vectorAdd(&projected, &(plane->p));?
  67.     ?
  68.     return projected;?
  69. }?
  70.  ?
  71. // Project a triangle onto a plane that intersects the origin.?
  72. Triangle projectOnPlane ( Triangle *t, Plane *p ) {?
  73.     // for each point in the triangle we need to generate a vector to ?
  74.     // that point and project the vector onto the plane. The three ?
  75.     // resulting vectors point to the projected triangle points.?
  76.     Vector3 a = projectPointOnPlane(&(t->a), p);?
  77.     Vector3 b = projectPointOnPlane(&(t->b), p);?
  78.     Vector3 c = projectPointOnPlane(&(t->c), p);?
  79.     ?
  80.     Vector3 displacement = pointNearestOrigin(p);?
  81.     ?
  82.     Triangle result = { vectorAdd(&a, &displacement), vectorAdd(&b, &displacement), vectorAdd(&c, &displacement) }; ?
  83.     return result;?
  84. }?
  85.  ?
  86. int main(int argc, char** argv) {?
  87.     Vector3 planePoint = { -1.0, -1.0, -1.0 };?
  88.     Vector3 planeNormal = { 0.1, 0.1, 0.1 };?
  89.     Plane plane = { planePoint, planeNormal };?
  90.  ?
  91.     Vector3 p1 = { -2.0, 1.0, 1.0 };?
  92.     Vector3 p2 = { 2.0, 1.0, 3.0 };?
  93.     Vector3 p3 = { 6.0, 1.0, 4.0 };?
  94.     Triangle triangle = { p1, p2, p3 };?
  95.     Triangle projectedT = projectOnPlane(&triangle, &plane);?
  96.     ?
  97.     printf("(%f,%f,%f)\n(%f,%f,%f)\n(%f,%f,%f)\n", projectedT.a.x, projectedT.a.y, projectedT.a.z,?
  98.                                                    projectedT.b.x, projectedT.b.y, projectedT.b.z,?
  99.                                                    projectedT.c.x, projectedT.c.y, projectedT.c.z);?
  100.     return 0;?
  101. }?
  102.  ?
© 2011 Code Tidy  Terms and conditions