Test for NaN as a conditional
in
Programming Questions
•
10 months ago
I'm practicing parametric intersections of a line and a circle. There can be 0, 1, or 2 intersections among the two. If there are less than 2 intersections I end up with NaN as one of my intersection variables (which is fine, the equation is trying to calculate an intersection that doesn't exist). What is the standard way to test for NaN? Right now I just am drawing the intersection point if its intersection variable is greater than 0:
- float theta = 0;
- float rx, ry;
- float cr, cx, cy;
- void setup() {
- size(600, 600);
- noFill();
- smooth();
- cr = 50;
- cx = 300;
- cy = 500;
- ry = height;
- }
- void draw() {
- background(255);
- line(0, 0, rx, ry);
- ellipse(cx, cy, cr*2, cr*2);
- theta += TWO_PI/1000;
- rx = (cos(theta)+1)*width/2;
- float t1 = (rx*cx+ry*cy + sqrt(cr*cr*(rx*rx+ry*ry)-sq(rx*cy-ry*cx)))/(rx*rx+ry*ry);
- float t1x = t1*rx;
- float t1y = t1*ry;
- if (t1 > 0) {
- ellipse(t1x, t1y, 5, 5);
- //println("t1 = "+t1);
- }
- float t2 = (rx*cx+ry*cy - sqrt(cr*cr*(rx*rx+ry*ry)-sq(rx*cy-ry*cx)))/(rx*rx+ry*ry);
- float t2x = t2*rx;
- float t2y = t2*ry;
- if (t2 > 0) {
- ellipse(t2x, t2y, 5, 5);
- //println("t2 = "+t2);
- }
- }
EDIT: t1 and t2 are the variables that are NaN when either one has no intersection
1