trigonometry

Inner angle between two lines

你说的曾经没有我的故事 提交于 2019-11-27 14:48:52
问题 I have two lines: Line1 and Line2. Each line is defined by two points (P1L1(x1, y1), P2L1(x2, y2) and P1L1(x1, y1), P2L3(x2, y3)) . I want to know the inner angle defined by these two lines. For do it I calculate the angle of each line with the abscissa: double theta1 = atan(m1) * (180.0 / PI); double theta2 = atan(m2) * (180.0 / PI); After to know the angle I calculate the following: double angle = abs(theta2 - theta1); The problem or doubt that I have is: sometimes I get the correct angle

Math.cos() gives wrong result

試著忘記壹切 提交于 2019-11-27 14:34:14
According to Wolfram Mathematica: cos(50) = 0.6427876096865394 ; But this code in Java: System.out.println(Math.cos(50)); gives 0.9649660284921133 . What is wrong with java.lang.Math ? Math.cos() expects the parameter to be in radians. This will return the result you need: Math.cos(Math.toRadians(50)); Math.cos() uses radians , so to get your expected result you need to do System.out.println(Math.cos(Math.toRadians(50))); Most Java trigonometric functions expects parameters to be in radians. You can use Math.toRadians() to convert: System.out.println(Math.cos(Math.toRadians(50))); Degrees <>

Calculating if an angle is between two angles

夙愿已清 提交于 2019-11-27 14:32:58
问题 So I am making a little game where I am checking if a character can "see" another where character A can see character B if A is within a certain distance of B, and the direction in degrees of A is +/- 45 degrees of the angle B is facing. Currently, I do a little calculation where I'm checking if (facingAngle - 45) =< angleOfTarget =< (facingAngle + 45) This works fine except for when we cross the 360 degree line. Let's say facingAngle = 359, angleOfTarget = 5 . In this situation, the target

Create sine lookup table in C++

这一生的挚爱 提交于 2019-11-27 12:24:30
问题 How can I rewrite the following pseudocode in C++? real array sine_table[-1000..1000] for x from -1000 to 1000 sine_table[x] := sine(pi * x / 1000) I need to create a sine_table lookup table. 回答1: You can reduce the size of your table to 25% of the original by only storing values for the first quadrant, i.e. for x in [0,pi/2]. To do that your lookup routine just needs to map all values of x to the first quadrant using simple trig identities: sin(x) = - sin(-x), to map from quadrant IV to I

How to convert x,y coordinates to an angle?

戏子无情 提交于 2019-11-27 11:07:37
问题 Microsoft provide an excellent SVG gradient maker so IE9 can also have "CSS3" gradients (click Custom). I currently utilise their logic for my Fireworks and Dreamweaver extensions to convert gradients to SVG, but I only know how to do it for standard top, bottom, left, right directions. If you enter an angle, I don't do the conversion, because I'm not sure how I would convert x1, x2, y1, y2 to CSS3 angle degrees. The gradient generator provides values like this: x1="0%" y1="0%" x2="56

Cheap algorithm to find measure of angle between vectors

帅比萌擦擦* 提交于 2019-11-27 10:00:38
问题 Finding the angle between two vectors is not hard using the cosine rule. However, because I am programming for a platform with very limited resources, I would like to avoid calculations such as sqrt and arccos . Even simple divisions should be limited as much as possible. Fortunately, I do not need the angle per se, but only need some value that is proportional to said angle. So I am looking for some computationally cheap algorithm to calculate a quantity that is related to the angle between

How to calculate rotation in 2D in Javascript

瘦欲@ 提交于 2019-11-27 10:00:34
问题 I am not so familiar trigonometry, but I have only two points to rotate in 2D: *nx, ny . - . - . angle - *cx,cy.................*x,y cx, cy = rotation center x,y = current x,y nx, ny = new coordinates How to calculate new points in a certain angle? 回答1: 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]; }

Javascript sin function issue

回眸只為那壹抹淺笑 提交于 2019-11-27 07:23:45
问题 I have a problem with Math.sin . I thought it would output the sinus of the given integer. So I tried Math.sin(30) and my output was -0.9880316240928618 and then I checked with my calculator and it was 0.5. 回答1: Parameters are assumed to be in radians, not degrees. Try Math.sin(Math.PI * (30/180)); A comment below notes that pre-computing the ratio π/180 is a good idea. One could add a companion to Math.sin that works on degrees this way: Math.dsin = function() { var piRatio = Math.PI / 180;

Moving an object along a straight line at a constant speed from point A to B

て烟熏妆下的殇ゞ 提交于 2019-11-27 06:14:09
问题 I know this has been asked a few times before, but there has been no answer that actually works that I can find. There is a similar one, but the speed varies depending on the distance travelled. So my problem is that I am trying to get an object (a player in this case) to move a long a straight line from point A to B at a constant speed. This is done by clicking on the player and dragging to where I want him to walk to, so it can be in any direction and over any distance. I have some code

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

那年仲夏 提交于 2019-11-27 05:12:45
问题 I am googling the question for past hour, but there are only points to Taylor Series or some sample code that is either too slow or does not compile at all. Well, most answer I've found over Google is "Google it, it's already asked", but sadly it's not ... I am profiling my game on low-end Pentium 4 and found out that ~85% of execution time is wasted on calculating sinus, cosinus and square root (from standard C++ library in Visual Studio), and this seems to be heavily CPU dependent (on my I7