trigonometry

Implementation of sinpi() and cospi() using standard C math library

女生的网名这么多〃 提交于 2019-11-30 14:51:47
问题 The function sinpi(x) computes sin(πx), and the function cospi(x) computes cos(πx), where the multiplication with π is implicit inside the functions. These functions were initially introduced into the C standard math library as an extension by Sun Microsystems in the late 1980s. IEEE Std 754™-2008 specifies the equivalent functions sinPi and cosPi in section 9. There are numerous computations where sin(πx) and cos(πx) occur naturally. A very simple example is the Box-Muller transform (G. E. P

Given start point, angles in each rotational axis and a direction, calculate end point

吃可爱长大的小学妹 提交于 2019-11-30 14:09:02
问题 I have a start point in 3D coordinates, e.g. (0,0,0). I have the direction I am pointing, represented by three angles - one for each angle of rotation (rotation in X, rotation in Y, rotation in Z) (for the sake of the example let's assume I'm one of those old logo turtles with a pen) and the distance I will travel in the direction I am pointing. How would I go about calculating the end point coordinates? I know for a 2D system it would be simple: new_x = old_x + cos(angle) * distance new_y =

How to compute correctly rounded trigonometric functions in degrees?

北慕城南 提交于 2019-11-30 13:58:26
How could I define trigonometric functions that take arguments in degrees instead of the usual radians, and compute correctly rounded results for these arguments? Multiplying the argument by M_PI/180.0 before passing it to the corresponding function in radians does not work, because M_PI/180.0 is not π/180. Section 5.5 of the Handbook of Floating-Point Arithmetic offers a method to compute the correctly rounded product of the argument by π/180, but some arguments will still be such that this product is close to the midpoint between two consecutive representable floats, and then applying even a

How to approximate a half-cosine curve with bezier paths in SVG?

六月ゝ 毕业季﹏ 提交于 2019-11-30 13:54:43
问题 Suppose I want to approximate a half-cosine curve in SVG using bezier paths. The half cosine should look like this: and runs from [x0,y0] (the left-hand control point) to [x1,y1] (the right-hand one). How can I find an acceptable set of coefficients for a good approximation of this function? Bonus question : how is it possible to generalize the formula for, for example, a quarter of cosine? Please note that I don't want to approximate the cosine with a series of interconnected segments, I'd

Why is std::sin() and std::cos() slower than sin() and cos()?

南笙酒味 提交于 2019-11-30 11:04:31
Test code: #include <cmath> #include <cstdio> const int N = 4096; const float PI = 3.1415926535897932384626; float cosine[N][N]; float sine[N][N]; int main() { printf("a\n"); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cosine[i][j] = cos(i*j*2*PI/N); sine[i][j] = sin(-i*j*2*PI/N); } } printf("b\n"); } Here is the time: $ g++ main.cc -o main $ time ./main a b real 0m1.406s user 0m1.370s sys 0m0.030s After adding using namespace std; , the time is: $ g++ main.cc -o main $ time ./main a b real 0m8.743s user 0m8.680s sys 0m0.030s Compiler: $ g++ --version g++ (Ubuntu/Linaro 4.5.2

Circle to Circle Segment Collision

懵懂的女人 提交于 2019-11-30 07:30:58
I'm struggling to find a rock solid solution to detecting collisions between a circle and a circle segment. Imagine a Field of View cone for a game enemy, with the circles representing objects of interest. The diagram at the bottom is something I drew to try and work out some possible cases, but i'm sure there are more. I understand how to quickly exlude extreme cases, I discard any targets that don't collide with the entire circle, and any cases where the center of the main circle is within the target circle are automatically true (E in the diagram). I'm struggling to find a good way to check

Accuracy of Math.Sin() and Math.Cos() in C#

荒凉一梦 提交于 2019-11-30 07:15:04
问题 I am terribly annoyed by the inaccuracy of the intrinsic trig functions in the CLR. It is well know that Math.Sin(Math.PI)=0.00000000000000012246063538223773 instead of 0. Something similar happens with Math.Cos(Math.PI/2) . But when I am doing a long series of calculations that on special cases evaluate to Math.Sin(Math.PI/2+x)-Math.Cos(x) and the result is zero for x=0.2, but not zero for x=0.1 (try it). Another issue is when the argument is a large number, the inaccuracy gets

Rotate point in rectangle

夙愿已清 提交于 2019-11-30 07:12:27
I have a point in a rectangle that I need to rotate an arbitrary degree and find the x y of the point. How can I do this using javascript. Below the x,y would be something like 1,3 and after I pass 90 into the method it will return 3,1. |-------------| | * | | | | | |-------------| _____ | *| | | | | | | | | _____ |-------------| | | | | | *| |-------------| _____ | | | | | | | | |* | _____ Basically I am looking for the guts to this method function Rotate(pointX,pointY,rectWidth,rectHeight,angle){ /*magic*/ return {newX:x,newY:y}; } This should do it: function Rotate(pointX, pointY, rectWidth

C++ (and maths) : fast approximation of a trigonometric function

眉间皱痕 提交于 2019-11-30 06:46:44
I know this is a recurring question, but I haven't really found a useful answer yet. I'm basically looking for a fast approximation of the function acos in C++, I'd like to know if I can significantly beat the standard one. But some of you might have insights on my specific problem: I'm writing a scientific program which I need to be very fast. The complexity of the main algorithm boils down to computing the following expression (many times with different parameters): sin( acos(t_1) + acos(t_2) + ... + acos(t_n) ) where the t_i are known real (double) numbers, and n is very small (like smaller

Angle between 3 points?

Deadly 提交于 2019-11-30 06:31:27
问题 Given points ABC, how could I find angle ABC? I'm making a feehand tool for a vector drawing application and to minimize the number of points it generates, I wont add a points unless the angle of the mouse position and the last 2 points is greater than a certain threshold. Thanks what I had: int CGlEngineFunctions::GetAngleABC( POINTFLOAT a, POINTFLOAT b, POINTFLOAT c ) { POINTFLOAT ab; POINTFLOAT ac; ab.x = b.x - a.x; ab.y = b.y - a.y; ac.x = b.x - c.x; ac.y = b.y - c.y; float dotabac = (ab