trigonometry

Calculating cosine values for an array in Python

给你一囗甜甜゛ 提交于 2019-12-10 09:27:47
问题 I have this array named a of 1242 numbers. I need to get the cosine value for all the numbers in Python. When I use : cos_ra = math.cos(a) I get an error stating: TypeError: only length-1 arrays can be converted to Python scalars How can I solve this problem?? Thanks in advance 回答1: Problem is you're using numpy.math.cos here, which expects you to pass a scalar. Use numpy.cos if you want to apply cos to an iterable. In [30]: import numpy as np In [31]: np.cos(np.array([1, 2, 3])) Out[31]:

How to get coordinates of a point in a coordinate system based on angle and distance

自作多情 提交于 2019-12-10 03:57:41
问题 How to get coordinates of a point in a coordinate system when all I have is the origin coordinates (x, y) and the angle from the origin to the point and the distance from the origin to the point? 回答1: You use Math.cos, Math.sin like this: pointX = x + distance * Math.cos(angle) pointY = y + distance * Math.sin(angle) Note that Math.cos and Math.sin assumes the argument is given in radians . If you have the angle in degrees, you would use Math.cos( Math.toRadians(angle) ) for instance. 回答2: If

Where is the sine function?

£可爱£侵袭症+ 提交于 2019-12-10 02:06:43
问题 Simple question: Where is sin() ? I've searched and only found in the Rust docs that there are traits like std::num::Float that require sin, but no implementation . 回答1: The Float trait was removed, and the methods are inherent implementations on the types now. That means there's a bit less typing to access math functions: fn main() { let val: f32 = 3.14159; println!("{}", val.sin()); } However, it's ambiguous if 3.14159.sin() refers to a 32- or 64-bit number, so you need to specify it

C: Improving performance of function with heavy sin() usage

梦想的初衷 提交于 2019-12-09 16:26:01
问题 I have a C function that computes the values of 4 sines based on time elapsed. Using gprof, I figured that this function uses 100% (100.7% to be exact lol) of the CPU time. void update_sines(void) { clock_gettime(CLOCK_MONOTONIC, &spec); s = spec.tv_sec; ms = spec.tv_nsec * 0.0000001; etime = concatenate((long)s, ms); int k; for (k = 0; k < 799; ++k) { double A1 = 145 * sin((RAND1 * k + etime) * 0.00333) + RAND5; // Amplitude double A2 = 100 * sin((RAND2 * k + etime) * 0.00333) + RAND4; //

Python Uniform distribution of points on 4 dimensional sphere

一个人想着一个人 提交于 2019-12-09 05:51:05
问题 I need a uniform distribution of points on a 4 dimensional sphere. I know this is not as trivial as picking 3 angles and using polar coordinates. In 3 dimensions I use from random import random u=random() costheta = 2*u -1 #for distribution between -1 and 1 theta = acos(costheta) phi = 2*pi*random x=costheta y=sin(theta)*cos(phi) x=sin(theta)*sin(phi) This gives a uniform distribution of x, y and z. How can I obtain a similar distribution for 4 dimensions? 回答1: A standard way, though, perhaps

how to extrude a path in 3d?

99封情书 提交于 2019-12-09 04:41:09
问题 I'm trying to extrude a path in 3d. Nothing fancy yet, just following some points and using a regular polygon for 'tubing'. I'm using Processing for now to quickly prototype, but will later turn the code into OpenGL. My problem is rotating the 'joints' at the right angles. I think I have a rough idea how to get the angles, not sure. I've started from a sample by Simon Greenwold(Processing > File > Examples > 3D > Form > Vertices).Here's my attempt so far: UPDATE > REFACTORED/SIMPLIFIED CODE

Rotating object to face mouse pointer on mousemove

99封情书 提交于 2019-12-08 22:40:52
问题 I've got a mousemove cursor in my game which will make my object shoot towards my mouse cursor. I'd like my object to always rotate along to be in line with my mousecursor. How can i convert the X and Y of where the cursor is to a degree angle to rotate my object to? I hope my fiddle will make things a little clearer of to what i mean by rotating the player(Black block): http://jsfiddle.net/3nEUv/4/ Here's my mouseMove function now; Only making sure the cursor remains in it's bounding box

Find corners of a rotated rectangle

混江龙づ霸主 提交于 2019-12-08 21:44:41
I'm trying to resize a rotated rectangle, you just drag the image out (or in) from 1 corner, and the corner diagonal of that 1 will stay in the old position. So I know the angle(radians) of rotation and 2 corners diagonal of each other, now I would like to find the other two corners. I've tried to calculate em with Trigonometry but I failed miserably, so how can u calculate those other 2 points. In pseudocode: r = (x2 - x1)*sin(a) - (y2 - y1)*cos(a) x3 = x1 + r*sin(a) y3 = y1 - r*cos(a) x4 = x2 - r*sin(a) y4 = y2 + r*cos(a) What this is doing is recovering the length r of the side of the

MySQL Bug? (Trigonometry)

大城市里の小女人 提交于 2019-12-08 18:03:24
问题 I was optimizing a query by precalculating some trigonometry-funnctions for the fields in a table, when I stumbled on this: SELECT 6371 * acos( 0.793521289617132 * 0.793521289617132 + 0.608542490648241 * 0.608542490648241 * cos( 0.235244203230056 - 0.235244203230056 ) ) returns null the query with non-precalculated values: SELECT 6371 * acos( sin( radians( 52.51581 ) ) * sin( radians( 52.51581 ) ) + cos( radians( 52.51581 ) ) * cos( radians( g.lat ) ) * cos( radians( 13.4785 ) - radians( 13

Computational cost of trig functions [duplicate]

别等时光非礼了梦想. 提交于 2019-12-08 14:36:01
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: How do Trigonometric functions work? What actually goes into the computation of trig functions like Sin, Cos, Tan and Atan? I think I've found an optimization in my code where I can avoid using any of these functions and base the problem around slope instead of angles. So that means a couple division operations in place of the above trig functions. But I'd like to know more about what goes into those trig