 # Most efficient way to draw hyperbola?

TLDR: What’s easier for a computer: making a curve out of a polygon with a bunch of regular vertices, or making it out of a shape with a couple of quadratic vertices?

So, as the title suggests, I need to generate a hyperbola (the 1/x graph). Specifically a hyperbola of arbitrary width and height (and rotation, and position).

There are two approaches I’ve taken to this: one is to draw an open polygon with a bunch of vertices (about 64), and the other is to draw a shape made of a few quadratic vertices (about 8). At first I just wanted to ask which approach would yield a more accurate approximation per unit time spent computing & rendering it all, but now that I think about it, I suppose I also have to ask if there are any more efficient approaches to this (there probably are).

For reference, I’ve optimized both approaches so that the computer runs a couple cosh and sinh functions to compute the first few vertices, and uses angle addition formulas to find the rest (so that most of the calculations are simple arithmetic). I also made an optimization so the computer will only look at the vertices that can be seen on screen, so more vertices can be displayed at once.

If someone could tell me which technique is faster, and if there are any approaches that would be even faster, that’d be great. Thank you!

EDIT: I forgot to mention before, but the shape in question needs to have a constant strokeWeight, and won’t be filled in.

2 Likes

I am not sure if I understand exactly what you want but you could modify the transformation matrix using `translate()`, `rotate()` and `scale()` between `pushMatrix()` and `popMatrix()` statements so do not have to recalculate the vertices if the curve’s position, size or rotation changes.

I suppose your right, this does require a bit of clarification. The reason I put position and rotation in parentheses was because I knew once everything else was figured out, I could just use matrix transformations and translations to do the position and angle.

In retrospect, I probably should’ve mentioned this before, but there’s another option that can be used, and a reason that it’s not a viable option: I could create an image of a hyperbola, have the computer display a skewed version of the image, and draw straight lines stemming from the point where the curvature becomes negligible. The reason it’s not a viable option, however, is that the resulting image would have an inconsistent strokeWeight(), and the image I need displayed needs a constant strokeWeight() (relatively speaking). This is also the same reason I can’t use the scale function here.

Forget the image - that is not a viable option for what you want to achieve.

Solving the scale problem is simple - set the stoke weight to `sw1 / scale` where `sw1` is the stroke weight used when the scale is `1.0` and `scale` is the current scale you are drawing at. As the scale increases the stroke weight decreases.

As I mentioned, though, it needs to have an arbitrary width AND height. I need to be able to draw the 1/x graph, but I also need to be able to draw a vertically stretched version of that, or a horizontally stretched version of that, or a vertically compressed, etc. If you still don’t understand what I’m implying, this shape is to the 1/x graph as an ellipse is to a circle. Now, if you tried using scale(2,1) and then drew a circle, you’d notice the strokeWeight varies throughout the entire shape, being fatter in some spots and thinner in others. That’s why we often use the ellipse feature, instead of just using scale() and drawing a circle every time.

In any case, however, the main problem is: what’s more efficient, a few quadratic vertices, or a bunch of polygonal vertices?

Were you able to resolve this?

If performance really matters, then I think the best answer is: test them both and see. Also, be aware that the results might depend on your graphics mode and hardware, so test on the target if you have one.