trigonometry

sin, cos, tan and rounding error

有些话、适合烂在心里 提交于 2019-11-28 00:22:21
I'm doing some trigonometry calculations in C/C++ and am running into problems with rounding errors. For example, on my Linux system: #include <stdio.h> #include <math.h> int main(int argc, char *argv[]) { printf("%e\n", sin(M_PI)); return 0; } This program gives the following output: 1.224647e-16 when the correct answer is of course 0. How much rounding error can I expect when using trig functions? How can I best handle that error? I'm familiar with the Units in Last Place technique for comparing floating point numbers, from Bruce Dawson's Comparing Floating Point Numbers , but that doesn't

Rotate a point around another point

安稳与你 提交于 2019-11-27 20:10:18
I have a task to draw a specific graphic. As part of this task I need to rotate some dot's on 45 degrees. I've spent already 2 days trying to calculate a formula, but just couldn't get it right. I've been searching all over the place including this particular website, I'm getting very close, but I'm still not there. Here it is: I need to draw 4 different points I have a specific formula to calculate there position, which is out of scope of the question, but here is what I'm getting as a result of it: int radius = 576; int diameter = radius * 2; Point blueA = new Point(561, 273); Point greenB =

2D Euclidean vector rotations

天大地大妈咪最大 提交于 2019-11-27 20:01:41
问题 I have a euclidean vector a sitting at the coordinates (0, 1) . I want to rotate a by 90 degrees (clockwise) around the origin: (0, 0) . If I have a proper understanding of how this should work, the resultant (x, y) coordinates after the rotation should be (1, 0) . If I were to rotate it by 45 degrees (still clockwise) instead, I would have expected the resultant coordinates to be (0.707, 0.707) . theta = deg2rad(angle); cs = cos(theta); sn = sin(theta); x = x * cs - y * sn; y = x * sn + y *

Sine Wave Sound Generator in Java

不问归期 提交于 2019-11-27 19:53:03
What's the simplest way to generate a sine wave sound at any frequency in Java? A sample size more than 2 bytes would help, but it doesn't really matter. Thanks Andrew Thompson See Beeper for a self-contained example. Perhaps something simpler? That 51 lines of snippet (repeated below - spaced out for single line & in-line comments) as shown at the top of the linked answer, is about as simple as generating a tone gets (OK, you can take out 5+ lines for the harmonic). People seem to assume it should be a method built into the toolkit to produce a pure tone. It is not, and takes a little

How can I work around the fact that in C++, sin(M_PI) is not 0?

為{幸葍}努か 提交于 2019-11-27 17:38:40
问题 In C++, const double Pi = 3.14159265; cout << sin(Pi); // displays: 3.58979e-009 it SHOULD display the number zero I understand this is because Pi is being approximated, but is there any way I can have a value of Pi hardcoded into my program that will return 0 for sin(Pi)? (a different constant maybe?) In case you're wondering what I'm trying to do: I'm converting polar to rectangular, and while there are some printf() tricks I can do to print it as "0.00", it still doesn't consistently

What is the method for converting radians to degrees?

随声附和 提交于 2019-11-27 16:57:28
I run into this occasionally and always forget how to do it. One of those things that pop up ever so often. Also, what's the formula to convert angles expressed in radians to degrees and back again? Dave Costa radians = degrees * (pi/180) degrees = radians * (180/pi) As for implementation, the main question is how precise you want to be about the value of pi. There is some related discussion here a complete circle in radians is 2*pi. A complete circle in degrees is 360. To go from degrees to radians, it's (d/360) * 2*pi, or d*pi/180. x rads in degrees - > x*180/pi x degrees in rads -> x*pi/180

Simple circle rotation (simulate motion)

邮差的信 提交于 2019-11-27 15:54:38
I have a simple animation in Java that consists of a wheel moving across a window. It is just a plain circle that starts off of the screen from the left, enters and continues to the right until it goes off of the screen. Then it loops and repeats this process. X is a variable that contains the position of the wheel. It can be between -(wheel width) and the window size + the wheel width. I would like to simulate rotation by drawing a circle within this wheel, that rotates around the circle as if it were attached. Imagine a bike wheel in real life with a red flag on the wheel. As the wheel

Determine if angle lies between 2 other angles

↘锁芯ラ 提交于 2019-11-27 15:48:58
问题 I am trying to figure out whether a angle lies between 2 other angles. I have been trying to create a simple function to perform this but none of my techniques will work for all possible values of the angles. Can you help me edit my function to correctly determine if a angle lies between 2 other angles? In the above picture; I use the green point as the central point, then I determine the angle of each line to the green point. I then calculate the angle of the black point to the green point.

Java Math.cos(Math.toRadians(<angle>)) returns weird values

馋奶兔 提交于 2019-11-27 15:25:47
I've got a little Problem with the Math.cos() method. I know, I have to convert the angle to Radians before using Math.cos() . But if I just do: System.out.println(Math.cos(Math.toRadians(90)); It outputs: 6.123233995736766E-17 Math.sin() is working well. From trigonometry: sin x ~= x, for small values of x sin x = cos x+pi/2 Because pi/2 can't be represented exactly in IEEE-754 Floating point, it means, that it must be off by some value x, i.e it is represented by pi/2 +- x, where x < the least significant bit in the floating point system. Which in this case is 2^-53 = 1.1102e-16. In this

How to calculate the angle of a vector from the vertical?

牧云@^-^@ 提交于 2019-11-27 14:59:49
问题 Im trying to find out the angle (in degrees) between two 2D vectors. I know I need to use trig but I'm not too good with it. This is what I'm trying to work out (the Y axis increases downward): I'm trying to use this code at the moment, but it's not working at all (calculates random angles for some reason): private float calcAngle(float x, float y, float x1, float y1) { float _angle = (float)Math.toDegrees(Math.atan2(Math.abs(x1-x), Math.abs(y1-y))); Log.d("Angle","Angle: "+_angle+" x: "+x+"