## Rotate a point around another point

The problem is int center = radius which you are setting int radius = 576. This doesn’t make sense as surely you are rotating about a point that should have an x and y location. Given you are rotating around the origin the center x and y should both be 0 not 576. So, given … Read more

## Fastest implementation of sine, cosine and square root in C++ (doesn’t need to be much accurate)

Here’s the guaranteed fastest possible sine function in C++: double FastSin(double x) { return 0; } Oh, you wanted better accuracy than |1.0|? Well, here is a sine function that is similarly fast: double FastSin(double x) { return x; } This answer actually does not suck, when x is close to zero. For small x, … Read more

Categories c++

## Fast transcendent / trigonometric functions for Java

Computer Approximations by Hart. Tabulates Chebyshev-economized approximate formulas for a bunch of functions at different precisions. Edit: Getting my copy off the shelf, it turned out to be a different book that just sounds very similar. Here’s a sin function using its tables. (Tested in C since that’s handier for me.) I don’t know if … Read more

## How to calculate rotation in 2D in Javascript

function rotate(cx, cy, x, y, angle) { var radians = (Math.PI / 180) * angle, cos = Math.cos(radians), sin = Math.sin(radians), nx = (cos * (x – cx)) + (sin * (y – cy)) + cx, ny = (cos * (y – cy)) – (sin * (x – cx)) + cy; return [nx, ny]; } … Read more

## Calculate second point knowing the starting point and distance

It seems you are measuring distance (R) in meters, and bearing (theta) counterclockwise from due east. And for your purposes (hundereds of meters), plane geometry should be accurate enough. In that case, dx = R*cos(theta) ; theta measured counterclockwise from due east dy = R*sin(theta) ; dx, dy same units as R If theta is … Read more

## Calculating the angle between two lines without having to calculate the slope? (Java)

The atan2 function eases the pain of dealing with atan. It is declared as double atan2(double y, double x) and converts rectangular coordinates (x,y) to the angle theta from the polar coordinates (r,theta) So I’d rewrite your code as public static double angleBetween2Lines(Line2D line1, Line2D line2) { double angle1 = Math.atan2(line1.getY1() – line1.getY2(), line1.getX1() – … Read more

## Rotate line around center point given two vertices

The coordinates of the center point (cx,cy) of a line segment between points (x1,y1) and (x2,y2) are: cx = (x1 + x2) / 2 cy = (y1 + y2) / 2 In other words it’s just the average, or arithmetic mean, of the two pairs of x and y coordinate values. For a multi-segmented line, … Read more

## Calculating the position of points in a circle

Given a radius length r and an angle t in radians and a circle’s center (h,k), you can calculate the coordinates of a point on the circumference as follows (this is pseudo-code, you’ll have to adapt it to your language): float x = r*cos(t) + h; float y = r*sin(t) + k;

## Java BigDecimal trigonometric methods

ApFloat is a library which contains arbitrary-precision approximations of trigometric functions and non-integer powers both; however, it uses its own internal representations, rather than BigDecimal and BigInteger. I haven’t used it before, so I can’t vouch for its correctness or performance characteristics, but the api seems fairly complete.