#include "math.h" #include "polygon.h" Polygon::Polygon(Material mat, int m) : GObject() { material() = mat; N = 0; //number used so far Max = m; //max number P = new Point[m]; } void Polygon::set(Point p) { if(N>(istream& s, Polygon& poly) { int m; s >> m; //number of points in this polygon poly.setMaxPoints(m); for(int i = 0; i> p; poly.set(p); } s >> poly.material(); return s; } void Polygon::print(ostream &s) { s << *this; } void Polygon::read(istream& s) { s >> *this; } static float myabs(float x) { if(x < 0.0) return -x; else return x; } static int maxCoeff(float a, float b, float c) //returns 0, 1 or 2 depending on whether |a|, |b|, or |c| is max { float absA = myabs(a); float absB = myabs(b); float absC = myabs(c); int s = 0; float max = absA; if(absB > absA){ s = 1; max = absB; } if(absC > max){ s = 2; } return s; } static float xVal(Point p, int s) //returns the appropriate x-coordinate depending on s { if(s==0) return p.y(); else return p.x(); } static float yVal(Point p, int s) //returns the appropriate y-coordinate { if(s==2) return p.y(); else return p.z(); } static float dxVal(Point p0, Point p1, int s) { return xVal(p1,s) - xVal(p0,s); } static float dyVal(Point p0, Point p1, int s) { return yVal(p1,s) - yVal(p0,s); } bool Polygon::intersect(Ray ray, float& t, Colour& colour) //intersects ray with a polygon { /*find the parametric value t at intersection point*/ float denom = evalPlaneEquation(ray.direction()) + D; if(denom <0.000000001 & denom > -0.00000001) return false; t = -evalPlaneEquation(ray.origin())/denom; /*intersection point*/ float x = ray.origin().x() + t*ray.direction().x(); float y = ray.origin().y() + t*ray.direction().y(); float z = ray.origin().z() + t*ray.direction().z(); Point p(x,y,z); int test =0; /*now need to check if p is inside the polygon*/ /*forget the coordinate with the biggest coefficient in the plane equation*/ float a = Nm.x(); float b = Nm.y(); float c = Nm.z(); int s = maxCoeff(a,b,c); x = xVal(p,s); //this is the representation of the point y = yVal(p,s); float dx,dy,x1,y1; for(int i = 1; i < N; ++i){//for each edge P[i-1] to P[i] //form the edge equation dx = dxVal(P[i-1],P[i],s); dy = dyVal(P[i-1],P[i],s); x1 = xVal(P[i-1],s); y1 = yVal(P[i-1],s); /*do the test*/ if((x-x1)*dy - (y-y1)*dx > 0.0) { test--; } else{ test++; } } //now we have to do the last edge dx = dxVal(P[N-1],P[0],s); dy = dyVal(P[N-1],P[0],s); x1 = xVal(P[N-1],s); y1 = yVal(P[N-1],s); if((x-x1)*dy - (y-y1)*dx > 0.0) { test--; } else{ test++; } //if it survived all that then... return (test%2 ==0); }