home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / oct93 / graphics / graphtal.lha / Graphtal / Polygon.C < prev    next >
C/C++ Source or Header  |  1992-11-17  |  2KB  |  112 lines

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