trigonometry

Taylor series expansion as constexpr

こ雲淡風輕ζ 提交于 2019-12-05 15:49:55
I'm trying to build a simple sine function using taylor series expansion that can be evaluated at compile time using C++14 constexpr . My code is compiling, but the compiler doesn't generate a constant. sine is defined as follows: template <int P, typename T = double> constexpr T sine(T x) { T result = x; for (int i = 1; i < P; ++i) result += power<T>(-1, i) * power<T>(x, 1 + 2 * i) / factorial<T>(1 + 2 * i); return result; } I can provide code for power and factorial if needed. They are trivial and also constexpr . I'm calling sine from within a loop like this: template <int N> void test

Fast, inaccurate sin function without lookup

时光毁灭记忆、已成空白 提交于 2019-12-05 11:34:39
For an ocean shader, I need a fast function that computes a very approximate value for sin(x). The only requirements are that it is periodic, and roughly resembles a sine wave. The taylor series of sin is too slow, since I'd need to compute up to the 9th power of x just to get a full period. Any suggestions? EDIT: Sorry I didn't mention, I can't use a lookup table since this is on the vertex shader. A lookup table would involve a texture sample, which on the vertex shader is slower than the built in sin function. It doesn't have to be in any way accurate, it just has to look nice. Use a

Calculating offsets after square rotated from corner

冷暖自知 提交于 2019-12-05 09:27:13
问题 I am wanting to calculate the 4 offsets from the point of rotation when I rotate a square. The axis of rotation is initially the top left of the square. When I perform a rotation I would like to know how far the shape will spead in all 4 directions (minX, minY, maxX, maxy). I currently have the general math: const rotation = .35 // radians = 20 degrees const size = 50 // size of original square const o1 = Math.round(size * Math.sin(rotation)) const o2 = Math.round(size * Math.cos(rotation))

uniform generation of 3D points on cylinder/cone

为君一笑 提交于 2019-12-05 08:11:29
I wish to randomly and uniformly generate points on a cylinder and a cone (separately). The cylinder is defined by its center, its radius and height. Same specifications for the cone. I am able to get the bounding box for each shape so I was thinking of generating points within the bounding box. However, I'm not sure how to project them onto the cylinder/cone or if this is the best idea. Any suggestions? Thanks. The cylinder case is trivial. If the cylinder of radius r > 0 and height h > 0 is the image of (x, y, z) = (r cos φ, r sin φ, z) on φ ∈ [0, 2π[ and z ∈ [-h/2, h/2], then simply choose

How to “snap” a directional (2D) vector to a compass (N, NE, E, SE, S, SW, W, NW)?

旧时模样 提交于 2019-12-05 08:01:00
I have a bunch of vectors normal to window surfaces in a 3D modelling software. Projected to the XY-Plane, I would like to know in which direction they are facing, translated to the 8 compass coordinates ( North , North-East , East , South-East , South , South-West , West and North-West ). The vectors work like this: the X axis represents East-West (with East being positive) the y axis represents North-South (with North being positive) thus (0, 1) == North (1, 0) == East (0,-1) == South (-1,0) == West Given a vector (x, y) I am looking for the closest of the 8 compass coordinates. Any ideas on

Cosine in floating point

☆樱花仙子☆ 提交于 2019-12-05 06:33:40
I am trying to implement the cosine and sine functions in floating point (but I have no floating point hardware). Since my processor has no floating-point hardware, nor instructions, I have already implemented algorithms for floating point multiplication, division, addition, subtraction, and square root. So those are the tools I have available to me to implement cosine and sine. I was considering using the CORDIC method, at this site However, I implemented division and square root using newton's method, so I was hoping to use the most efficient method. Please don't tell me to just go look in a

Find the rotation angles of a triangle in 3D, given the coordinates of its vertices

守給你的承諾、 提交于 2019-12-05 06:12:39
问题 I try to rotate and translate an equilateral triangle in 3D until his vertices reach some coordinates. The vertices coordinates F,G,H and F',G',H' are known : I was able to find the new centroid c' coordinates like this : c'.x = ( F'.x + G'.x + H'.x ) / 3 c'.y = ( F'.y + G'.y + H'.y ) / 3 c'.z = ( F'.z + G'.z + H'.z ) / 3 So no problem to translate the triangle. But I can't find a way to calculate the rotations needed to put F'G'H' triangle in the right position... I have to know by how much

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

时光怂恿深爱的人放手 提交于 2019-12-05 05:45:44
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? 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. If r is the distance from origin and a is the angle (in radians) between x-axis and the point you can easily

Calculate saw and triangle wave from specific data

試著忘記壹切 提交于 2019-12-05 05:33:32
问题 I need to calculate a triangle and saw wave but it is a little complicate because of my model and the data I'm able to work with (but maybe I'm just confused). I'm able to calculate my sine wave but I'm not really using a frame counter. What I do is, calculate a theta_increment variable which I can use the next time I need to calculate a sample. This works like this: float x = note.frequency / AppSettings::sampleRate; float theta_increment = 2.0f * M_PI * x; float value = 0; if(waveType ==

Where is the sine function?

余生长醉 提交于 2019-12-05 02:35:38
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 . 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 explicitly. Above, I set the type of the variable, but you can also use a type suffix: fn main() { println!("{}",