public bool intersectionTriangle(ray r, Vector2 v, primitives p) { //box pixelcoordinates to world coordinates float tx = TX(p.X); float ty = TY(p.Y); float tw = TW(p.W); float th = TW(p.H); //3 vectors, for every line of the triangle 1 Vector2 blc = new Vector2(tx, ty + th); Vector2 brc = new Vector2(tx + tw, ty + th); Vector2 tc = new Vector2(tx + (tw / 2), ty); ////ensure that it is not inside the triangle. Else you have an intersection immediatly. //if (p.X > blc.X && p.X < trc.X && p.Y < blc.Y && p.Y > trc.Y && r.o.X > blc.X && r.o.X < trc.X && r.o.Y < blc.Y && r.o.Y > trc.Y) //{ // return true; //} // check for intersection for every vector of the triangle. if (intersectionLine(r, v, blc, tc)) { return(true); } if (intersectionLine(r, v, tc, brc)) { return(true); } if (intersectionLine(r, v, brc, blc)) { return(true); } return(false); }
public bool intersectionBox(ray r, Vector2 p, primitives b) { //box pixelcoordinates to world coordinates float bx1 = TX(b.X); float by1 = TY(b.Y); float bx2 = (TX(b.X)) + (TW(b.W)); float by2 = (TY(b.Y)) + (TW(b.H)); //4 vectors, for every line of the box 1 Vector2 blc = new Vector2(bx1, by2); Vector2 brc = new Vector2(bx2, by2); Vector2 tlc = new Vector2(bx1, by1); Vector2 trc = new Vector2(bx2, by1); //ensure that it is not inside the box. Else you have an intersection immediatly. if (p.X > blc.X && p.X < trc.X && p.Y < blc.Y && p.Y > trc.Y && r.o.X > blc.X && r.o.X < trc.X && r.o.Y < blc.Y && r.o.Y > trc.Y) { return(true); } // check for intersection for every vector of the box. if (intersectionLine(r, p, tlc, blc)) { return(true); } if (intersectionLine(r, p, blc, brc)) { return(true); } if (intersectionLine(r, p, tlc, trc)) { return(true); } if (intersectionLine(r, p, trc, brc)) { return(true); } return(false); }
public bool intersectionCircle(ray r, Vector2 v, primitives p) { float radius = ((TW(p.W) / 2)); Vector2 vec = new Vector2(TX(p.X), TY(p.Y)); Vector2 c = vec - r.o; float t = Vector2.Dot(c, r.d); Vector2 q = c - (t * r.d); float p2 = Vector2.Dot(q, q); float tocircle = t - (float)Math.Sqrt(((radius * radius) - p2)); if (tocircle > r.t || t < 0) { return(false); } if ((radius * radius) < p2) { return(false); } else { r.t = t; return(true); } }