cloister
Full Member
Offline
Posts: 138
Re: polar coordinate system
Reply #5 - Mar 10th , 2009, 6:22pm
Lookup tables can (and do) speed up trig calculations, but at a cost: reduced accuracy. Basically, you have to decide ahead of time how big to make your tables, and later if you end up needing a value that's between the one in table slot N and table slot N+1, you're stuck. You have to just pick one. Essentially, it means that you have to round all your angles up or down to the nearest value that's actually represented in the lookup table. This may or may not actually be a problem, depending on the nature of the functions being plotted and the region of interest. For areas close to the polar coordinate origin, it's not a big deal. The perceptual difference between the point r=3, theta = 95 degrees and the point r=3, theta = 100 degrees is very small. For small values of radius, all the angles are packed close together, so you'll never see the small errors induced by the rounding-off that a lookup table creates. But for r=300, say, you'll definitely see the difference. But these days, honestly, the overhead of a sin() and cos() per point plotted is probably small enough as to be not worth worrying about. What I would suggest is that JanDot implement his/her own polar coordinate transforms as a set of utility functions within the sketch, and then route all drawing operations through those. So instead of calling, for example, "point(100, 300)" to plot a point at pixel x=100, y=300, you would call "polarPoint(57, 223.4)" (or whatever the corresponding polar coordinates were). polarPoint would simply do something like this: void polarPoint(float r, float theta) { point(r*cos(theta), r*sin(theta)); } Basically, just encapsulate all the cartesian-to-polar and polar-to-cartesian transformations inside a small set of easy-to-use functions that, except for their names, look like the underlying processing drawing functions. You'd also want, of course a pair of polarMouseR() and polarMouseTheta() functions that do the atan2() and distance calculation to convert from mouseX and mouseY. If it were me, I'd wrap all this functionality up in a class so that the constructor could handle all the business of setting things up: translating the cartesian coordinate system to wherever you wanted the origin to be (e.g. the center of the screen), and establishing an appropriate scale factor so that the desired amount of radius fits onto the screen. That way, if you needed to, you could have several independent polar coordinate systems operating simultaneously without getting lost in the details of which is which. I'm not sure what a useful application of that would be, but I'm sure there are some...