c#,trigonometry,floating-point-precision

Did you notice how much the two values are close of 0, and close to each other ? The floating point (im)precision and the implementation of each methods may probably perfectly explain that. Those methods are not perfect, for example they are relying on an approximation of Pi (of course,...

Use the function angle = atan2(y, x) Here's a link to more info: http://www.cplusplus.com/reference/cmath/atan2/ OR http://en.cppreference.com/w/cpp/numeric/math/atan2...

I think the code is much too complicate for what it does. If you know the figure is a square, all you need to do to find the centre is compute the midpoint between any two opposite corners. mid[0] = (points[0][0] + points[2][0]) / 2; mid[1] = (points[0][1] + points[2][1])...

c,floating-point,trigonometry,radians

double atan(double x); Returns the principal value of the arc tangent of x, expressed in radians. The output is in radians, not degrees. You need to convert the result of the function to degrees. The input remains a simple unitless number. #define R2D(trig_fn, val) (trig_fn(val) * (180.0 / M_PI))...

winforms,f#,trigonometry,pie-chart

Giving this a go by adding comments, not necessarily worked out in this order: // startAngle is the angle in degrees of this segment, angle is the angle of // the segment itself. let drawLabel (gr: Graphics) title startAngle angle = // So this is the angle of the centre...

python,trigonometry,angle,atan2,cartesian-coordinates

The easiest and most logically way to get at this problem is using the dot-product. Try this code (I've commented practically everything): import math def dot(vA, vB): return vA[0]*vB[0]+vA[1]*vB[1] def ang(lineA, lineB): # Get nicer vector form vA = [(lineA[0][0]-lineA[1][0]), (lineA[0][1]-lineA[1][1])] vB = [(lineB[0][0]-lineB[1][0]), (lineB[0][1]-lineB[1][1])] # Get dot prod dot_prod...

Update: Figured it out! I didn't need to add the centerX and centerY to each calculation, because in my code, those points were already relative to the center of the circle. So, while the canvas center was at point (300, 175), all points were relative to the circle that I...

javascript,canvas,trigonometry,raytracing,radians

First thing to do is to clarify your code. 1) you don't need to fill the array in reverse order. 2) use atan2 - i didn't get your way of dealing with radians... 3) cache the array element that you will re-use. 4) do not create one sort function on...

opengl,glsl,shader,trigonometry,double-precision

My current accurate shader implementation of 'acos()' is a mix out of the usual Taylor series and the answer from Bence. With 40 iterations I get an accuracy of 4.44089e-16 to the 'acos()' implementation from math.h. Maybe it is not the best, but it works for me: And here it...

javascript,html5,canvas,html5-canvas,trigonometry

Yes, you can use arcTo(): Set your first line start point with moveTo() then the intersection point between the two lines as first pair then the end point of the second line (what you call "startpoint line 2") the last pair. Provide a radius To actually draw the last line...

From the sample code it seems like what it mean by an edge is the vector from the originating vertex to the end vertex. In the case of a triangle with three vertices C1, C2, C3, the two input edges would be P1 = C2-C1, P2=C3-C1

python,coordinates,distance,trigonometry,angle

You just need a function that converts degrees to radians. Then your function simply becomes: from math import sin, cos, radians, pi def point_pos(x0, y0, d, theta): theta_rad = pi/2 - radians(theta) return x0 + d*cos(theta_rad), y0 + d*sin(theta_rad) (as you can see you mixed up sine and cosine in...

In the interest of posterity, in case anyone ever needs an answer to this question: I wanted a cosine whose frequency was a time-varying function freq(t). My mistake was simply evaluating this function at each time t like this: Acos(2pifreq(t)t). Instead you need to integrate freq(t) from 0 to t...

javascript,math,2d,trigonometry

The pivot of a rectangle is originally at position (x + pivotX, y + pivotY). Take the vector pointing from the red pivot point towards the green pivot point, namely vx = green.x + green.pivotX - red.x - red.pivotX; vy = green.y + green.pivotY - red.y - red.pivotY; That's the...

alpha.x = arccos(x) beta.y = arccos(y) gamma.z = arccos(z) This assumes alpha.x is the angle from the +x axis, beta.y with +y, gamma.z with +z. This is derived simply from the dot product formula: (x,y,z) * (1,0,0) = cos(alpha.x) from which the first formula follows. Same with 2nd and 3rd....

javascript,math,trigonometry,sin

You can know the angle of any sin with this formula: Math.asin(sinOfAngleX) * 180/Math.PI With sinOfAngleX = 0.5, Math.asin(sinOfAngleX) would give 0.5235987755982989. This is expressed in radians. To pass it to degrees you can multiply by 180/Math.PI, which results in 30º...

sprite-kit,trigonometry,skspritenode,cgpoint

I solved it after trying some more and here's how i did it: - (CGVector)convertAngleToVector:(CGFloat)radians { CGVector vector; vector.dx = cos(radians) * 40; vector.dy = sin(radians) * 40; return vector; } I call the method with the sprites zRotation which gives me a vector. The number 40 decides how long...

svg,graphics,trigonometry,bezier-curve,spline

After few tries/errors, I found that the correct ratio is K=0.37. "M" + x1 + "," + y1 + "C" + (x1 + K * (x2 - x1)) + "," + y1 + "," + (x2 - K * (x2 - x1)) + "," + y2 + "," + x2...

javascript,canvas,html5-canvas,automatic-ref-counting,trigonometry

The arc() method works only with angles so points has to be converted based on their location and distance to center (distance, representing the radius, has to be the same for both in this case). The signature of arc() is: void arc(unrestricted double x, unrestricted double y, unrestricted double radius,...

javascript,html5-canvas,trigonometry,2d-games

Man it took me longer than I'd like to admit to find the pattern for the hexagonal circles. I'm too tired right now to explain since I think I'll need to make some assisting illustrations in order to explain it. In short, each "circle" of hexagonal shapes is itself hexagonal....

javascript,trigonometry,rounding-error

Like you said, i/8 has an exact binary representation. Therefore (i/8) * ((2*Math.PI) / (2*Math.PI)) will not suffer from floating point rounding error, but (i/8*2*Math.PI) / (2*Math.PI) will. This is due to the fact that the intermediate result (i/8*2*Math.PI) has no exact floating point representation. Having said that, why do...

c,windows,winapi,screen,trigonometry

The coordinate system isn't quite what you're expecting: y == 0 is the top, not the bottom, of the screen. See Windows Coordinate System: The x-coordinates increase to the right; y-coordinates increase from top to bottom. The following illustrates it nicely (it talks about Java coordinates, but Windows coordinates are...

A possible cause is simply because your Player texture (the png/jpg itself) may not be pointing to the right, which is considered the 0 degree angle. If that's the case the easiest solution is to edit the texture so that the player points to the right. All of your code...

So, I ended up replacing double angle = getAngle(startPoint,endPoint)- Math.PI/2.0; with something more like double angle = -Math.atan2(endPoint.y - startPoint.y, endPoint.x - startPoint.x); I wrote this little test program, which allows you to move to points around a circle and which generates the resulting triangle... import java.awt.BorderLayout; import java.awt.Color; import...

algorithm,math,graphics,sprite-kit,trigonometry

To keep this as general as possible, I would go for a dichotomic search. That way you can be completely agnostic about your paths, as long as you have a black-box function that tells you whether paths intersect. This comes to mind and would be really simple because you only...

math,graphics,language-agnostic,collision-detection,trigonometry

I can imagine 4 different resolutions of what you have, and it is unclear which one you want. Here they are, where the black arrow is the movement of the centre of the ball between the frame before collision and the frame you are asking how to draw. A) the...

java,math,trigonometry,polynomial-math,equation-solving

I had two mistakes. All my "integer constants" should have been in double. In the Wikipedia method, when the depressed quartic is biquadratic, we must reconvert the roots to the original quartic. I was returning the depressed roots. Here is my resulting implementation public class QuarticFunction { private static final...

The easiest way is to build a class that models a point in both Cartiesian and polar coordinates. You can then use (x1, x2) -> to Polar -> add the angle -> to Cartesian. Alternatively, you can use the generalised rotation matrix: / \ / \/ \ |x2| = |...

Ok, first off to answer your question, the SyntaxError is caused by improper class instantiation. class declaration must be separated from the constructor method in python, like so: class Sphere(object): def __init__(self, radius, tx=0, ty=0, tz=0, sx=0, sy=0, sz=0): self.diameter = 2 * radius Tip: In the maya script editor...

javascript,jquery,canvas,geometry,trigonometry

The simplest way is to adjust the starting & ending angles of the arc command. You can draw a series of arc's to form your flower. Hint: If you want your petals to be less round and more curved, you might use quadraticCurveTo instead of arcs to draw the outside...

javascript,html5-canvas,fabricjs,trigonometry

I have changed the value of centerX and CenterY to center and its coming perfectly. Updated code should look like this. Your updated code http://jsbin.com/nepiqaviqe/1/edit?js,output var triangle = new fabric.Triangle({ angle: angle, fill: '#207cca', top: y2, left: x2, height: headLength, width: headLength, originX: 'center', originY: 'center', selectable: false }); fCanvas.add(triangle);...

It's not an answer to the original question; I am rather trying to resolve the issues between two answers and it wouldn't fit into a comment. The trigonometric approach is 4x slower than your original version with the square-root function on my machine (Linux, Intel Core i5). Your mileage will...

python,for-loop,numpy,trigonometry

numpy.arctan2() expect two arguments, and you're supplying just one (a tuple). You need to remove one pair of parentheses: theta = -np.arctan2((YA[:,:,i],XA2[i,:,:])) ^--------------------^ THESE ...

This issue is because of the finite floating point precision (not all real numbers can be represented exactly and not all calculations with them are precise). An accessible guide is in the Python docs. Using the default, "double precision" floating point representation, you can never hope for better than about...

math,microcontroller,trigonometry,atmega,approximation

Some of the terms being truncated to zero is not necessarily a disaster; this doesn't substantially worsen your approximation. I simulated your fixed precision setup in Matlab by rounding each term of the polynomial to the nearest integer: q4 = @(X) round((8.11 * X.^4)/2^34); q3 = @(X) -round((19.62* X.^3)/2^24); q2...

android,math,trigonometry,angle

If you want to find angle between vectors (point-previous) and (point-now), use: Math.atan2((now.x - point.x)*(previous.y - point.y) - (now.y - point.y)*(previous.x - point.x), (now.x - point.x)*(now.y - point.y) - (previous.x - point.x)*(previous.y - point.y)) (This is atan2(vectorproduct, scalarproduct)) You may also use angle = atan2(now.y-point.y, now.y-point.y)- atan2(previous.y-point.y, previous.x-point.x); ...

java,math,2d,physics,trigonometry

public void moveForward() { ySpeed += 0.01 * Math.sin(Math.toRadians(targetAngle)); xSpeed += 0.01 * Math.cos(Math.toRadians(targetAngle)); double currentSpeed = Math.sqrt(xTempSpeed * xTempSpeed + yTempSpeed * yTempSpeed); if (currentSpeed > maxSpeed) { //the resulting speed is allways <= maxspeed (normed to that) ySpeed *= maxSpeed/currentSpeed; xSpeed *= maxSpeed/currentSpeed; } hope this is...

Remember, atan2(x, z) returns the angle between the z-axis and the vector (x, z): But with atan2(0, 0), you're trying to find the angle between a point and either the z- or x-axis, which is meaningless: Even though atan2(0, 0) is meaningless, most programming language math libraries have atan2(x, y)...

android,canvas,gps,trigonometry

After some research time, I came to a successful outcome. I will comment on my thoughts and how was the solution. As I explained in question, along the way I have the coordinates traveled by the vehicle, and also a setting for the width of the path should be drawn....

You can set axes=FALSE in the plot() function and then define your own axes using axis(). Something like: x <- 1:100 set.seed(121) y <- rnorm(5) #random data atan.add <- atan(x/y) plot(atan.add,axes=FALSE) #note the "axes=FALSE" axis(side=1) #plot x axis (side=1) axis(side=2, at=c(-pi, -pi/2, -pi/4, 0 ,pi/4, pi/2, pi), labels=expression(-pi, -pi/2, -pi/4,...

objective-c,math,trigonometry,angle

The trig formula is tan(angle) = opposite / adjacent So to get the angle from the side lengths, you need to use the inverse tangent, which is atan2. double angle = atan2(opposite, adjacent); From there the rest of your code works as long as you know that atan2 returns an...

Instead of, for instance, a < wrap(b-c) use 0 < wrap(b-c-a) or 0 > wrap(a-b+c) This makes it a little less readable, but is the correct way to compare (supposedly small) angle differences....

python,opengl,geometry,trigonometry,pyglet

You need to change the degree to radian... (Degree/180)*pi is what you need

To search for gameobject within certain distance, and angle I messed around a lot and found a solution that I think works: GameObject[] nodesInView = GameObject.FindGameObjectsWithTag ("Node"); List<GameObject> listOfNodesInView = new List<GameObject> (); foreach (GameObject node in nodesInView) { float dist = (player.transform.position - node.transform.position).magnitude; if(dist < 300) { Vector3...

All mathematical functions from <math.h> are available in Swift if you import Darwin (which is automatically imported if you import Foundation or UIKit). Now the inverse cotangent function is not among these, but you can easily compute it from atan() using the relationships (see for example Inverse trigonometric functions): arccot(x)...

python,vector,simulation,trigonometry

Try fixing the boundary condition where alpha+beta and beta-alpha lies on either side of the positive x-axis. Assuming all your angles, beta and theta lie in the range (0, 2*Pi), this might work : def checkMargingForTheta(alpha, beta, theta): low = beta - alpha high = beta + alpha if(high >...

Looks like a perfect use for Vector2.Lerp(position, goal, amount) position == where the object is currently. goal == where you want the object to smoothly go to. amount == the percentage of the way it will cover this frame. So the amount is set to somewhere between 0 and 1....

math,vector,box2d,game-physics,trigonometry

First off, you should take the angle of the target relative to the turret, not the other way around. This can be fixed by swapping the position vectors in the subtraction. Second, once you have the target angle, subtracting the turret's current angle will give you the rotation offset you...

This can be solved through 2 applications of Pythag. Each side of your larger square is split into two by a corner of your small blue square. Lets call the larger of these 2 sections length a, the smaller length b (although if x > 45 degrees then b will...

java,math,trigonometry,degrees,radians

I believe you are overcomplicating this. If your starting point is (sx, sy) and your destination is (dx, dy) then you can easily calculate any point (x, y) that is p distance along the line (0.0 <= p <= 1.0). You can use this to move at velocity v. So...

javascript,image,rotation,crop,trigonometry

You can setup a line equation for the top and bottom side of the rotated rectangle: y1 = mx + b1, resp. y2 = mx + b2 m is the slope, which is the tangent of the angle between the line and the x-axis. b is the intercept (the y-value...

Math.Cos expects inputs in radians, not degrees: public static double Cos(double d) Parameters d An angle, measured in radians. You said My calculations show 75 * Cos(90) as zero so presumably you are working in degrees. You therefore need to use private static double DegreeToRadian(double angle) { return angle /...

.net,vb.net,gdi+,circle,trigonometry

The correct formulas are x = Sin(Angle) * LengthOfString y = Cos(Angle) * LengthOfString With 0 <= Angle <= 2*pi ...

If you have a Fourier series (i.e. f_i = i f for some f) you can use the Clenshaw recurrence relation which is significantly faster than computing all the sines (but it might be slightly less accurate). In your case you can consider the sequence: f_k = exp( i (...

You can implement your own functions in SQLite. For example, write a SQLite C function wrapper for acos: void sqlite_acos(sqlite3_context *context, int argc, sqlite3_value **argv) { int dataType = sqlite3_value_numeric_type(argv[0]); if (dataType == SQLITE_INTEGER || dataType == SQLITE_FLOAT) { double value = sqlite3_value_double(argv[0]); sqlite3_result_double(context, acos(value)); } else { sqlite3_result_null(context); }...

Octave does not have a native implementation of sec, it uses cos to do so. Check edit sec.m for details. The difference is caused by function calls of nargin in sec function call overhead for sec dividing the values To get rid of the first two factors, use 1./cos(x) instead...

javascript,html5,canvas,trigonometry

Quadratic curve formula: y=ax^2+bx+c // where a,b,c are known Line formula: // note: this `B` is not the same as the `b` in the quadratic formula ;-) y=m*x+B // where m,B are known. The curve & line intersect where both equations are true for the same [x,y]: Here's annotated...

The following code demonstrates the Taylor series (about x==0) for the sin() function. As you know, the sine function repeats an identical cycle for every 2*pi interval. But the Taylor series is just a polynomial -- it needs a lot of terms to approximate a wiggly function like sine. And...

This has a lot to do with the tutorial maker, and how he has decided to use spherical coordinates to generate his viewing angles. His approach is interesting, but remember that you can come up with your own! // Direction : Spherical coordinates to Cartesian coordinates conversion glm::vec3 direction( cos(verticalAngle)...

math,geometry,coordinates,trigonometry

If the given angle is φ, try: cx = ((ax-bx)*COS(2φ)+(by-ay)*SIN(2φ)-ax-bx)/2 cy = ((ay-by)*COS(2φ)+(ax-bx)*SIN(2φ)-ay-by)/2 Why? I calculated the coordinates of the mirror of B about the black line and defined C as the midpoint between B and its mirror. The rest is trig. Example: [ax,ay] = [7,1] [bx,by] = [6,4] φ...

Basic trigonometry or application of a rotation matrix (not to forget: translate the center to the origin and after rotation back to its inital position): XM=a+cos(C°)*(X1-a)-sin(C°)*(Y1-b) YM=b+sin(C°)*(X1-a)+cos(C°)*(Y1-b) The input to the trigonometric functions has to be in radians, 1°=pi/180, C°=C*1°. In screen coordinates, you have to first replace all y...

Math.atan2 returns its value in radians, not degrees. To convert to degrees, multiply by either 180/pi or 360/(2 pi): float ans = angle * 180 / Math.PI; Java even has a builtin function for this, Math.toDegrees: (thanks @yshavit) float ans = Math.toDegrees(angle); Also, what @yshavit says in the comments: you...

javascript,jquery,html5,canvas,trigonometry

Change line var degrees = -Math.atan2(offsetY,offsetX); to var degrees = Math.atan2(offsetY,-offsetX); Live Demo...

android,coordinates,trigonometry

I did not analyse where exactly the problem with your formula is hidden. But if you are just looking for a working example, you can find Android code in the accepted answer of this SO question....

javascript,canvas,html5-canvas,trigonometry

To calculate the direction from enemy to player you can simplify the calculations a little. Find direction angle var diffX = Player.x - Enemy.x, // difference in position diffY = Player.y - Enemy.y, angle = Math.atan2(diffY, diffX); // atan2 will give the angle in radians Notice also difference for Y...

java,geometry,line,point,trigonometry

I'd avoid all that trigonometry. For one of the edge directions, you want a vector which is at the same time perpendicular to V1 − V2 and to the Y axis. The cross product can be used to obtain a vector which is perpendicular to two given ones. So you...

All your sides are getting set to zero, because here: double hypotenuse = (3/4)*d; (3/4) equals 0. Change to: double hypotenuse = (3/4.0)*d; or: double hypotenuse = 0.75 * d; ...

html5,math,canvas,trigonometry

Your formula (rotation matrix) is correct but you are using reference values which means you are updating the values in the first part of the calculations and then the rest of the calculations uses that new value instead of the one you intend. One way to fix this is to...

Taking k_g's answer, we can actually shorten it quite a bit given that Vector2f has operator overloads. It can simply become: sf::Vector2f findVel(const sf::Vector2f& aPos, const sf::Vector2f& bPos, float speed) { sf::Vector2f disp = bPos-aPos; float distance = sqrt(disp.x*disp.x+disp.y*disp.y); // std::hypot(disp.x, disp.y) if C++ 11 return disp * (speed/distance); }...