home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / graphtal / polygon.c < prev    next >
C/C++ Source or Header  |  1992-10-19  |  2KB  |  111 lines

  1. /*
  2.  * Polygon.C
  3.  *
  4.  * Copyright (C) 1992, Christoph Streit (streit@iam.unibe.ch)
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  */
  17.  
  18. #include "Polygon.h"
  19.  
  20. implementList(VertexList, Vector);
  21. implementList(PolygonList, PolygonPtr);
  22.  
  23. Polygon::Polygon(long size)
  24. {
  25.   vertices = new VertexList(size);
  26. }
  27.  
  28. Polygon::Polygon(const Vector& p1, const Vector& p2, const Vector& p3)
  29. {
  30.   vertices = new VertexList(3);
  31.   vertices->append(p1);
  32.   vertices->append(p2); 
  33.   vertices->append(p3);
  34. }
  35.  
  36. Polygon::Polygon(const Vector& p1, const Vector& p2, 
  37.          const Vector& p3, const Vector& p4)
  38. {
  39.   vertices = new VertexList(4);
  40.   vertices->append(p1);
  41.   vertices->append(p2); 
  42.   vertices->append(p3);
  43.   vertices->append(p4);
  44. }
  45.  
  46. Polygon::Polygon(const Polygon& p)
  47. {
  48.   vertices = new VertexList(p.numVertices());
  49.   for (register long i=0; i<p.numVertices(); i++)
  50.     vertices->append(p.vertices->item(i));
  51. }
  52.  
  53. Polygon::~Polygon()
  54. {
  55.   delete vertices;
  56. }
  57.  
  58. /*
  59.  * Transform the vertices of the polygon.
  60.  */
  61.  
  62. void Polygon::transform(const TransMatrix& tmat)
  63. {
  64.   for (register long i=0; i < vertices->count(); i++)
  65.     vertices->item(i) = vertices->item(i)*tmat;
  66. }
  67.  
  68. /*
  69.  * Compute the normal of the polygon.
  70.  */
  71.  
  72. const Vector& Polygon::normal() const
  73. {
  74.   static Vector noNormal(0,0,0);
  75.   if (vertices->count() < 3)
  76.     return noNormal;
  77.  
  78.   static Vector n;
  79.   Vector p1 = vertices->item(0);
  80.   Vector p2 = vertices->item(1);
  81.   Vector p3 = vertices->item(vertices->count()-1);
  82.  
  83.   /*
  84.    * Find 3 vertices and compute the cross product. If the 
  85.    * resulting vector is not of yero length, take it as the 
  86.    * normal of the polygon. Otherwise continue.
  87.    */
  88.   for (register long i=1; i < vertices->count(); i++) {
  89.     n = (p2-p1)*(p3-p1);
  90.     if (!n.zero()) {
  91.       n.normalize();
  92.       return n;
  93.     }
  94.     p3 = p1; p1 = p2; p2 = vertices->item((i+1)%vertices->count());
  95.   }
  96.  
  97.   return noNormal;
  98. }
  99.  
  100. void Polygon::addVertex(const Vector& v)
  101. {
  102.   vertices->append(v);
  103. }
  104.  
  105. void Polygon::removeVertex(long index)
  106. {
  107.   vertices->remove(index);
  108. }
  109.  
  110.  
  111.