math,3d,camera,projection-matrix , 3D : keep horizon drawn on screen at the same height regardless camera's zoom

## Question:

Tag: math,3d,camera,projection-matrix

I have this strange use case.

These variables are known:

• Camera zoom (or fov)

• Camera rotation (orientation) around Y and Z axis `= 0`

Now, I want the horizon (horizon position `= (0,0,Infinite)`) to be drawn on screen at a specific 2D height "YY". What must be the Camera X-axis rotation so that the horizon is drawn at "YY"?

You might ask why may I need this: well, when I change the camera zoom, the horizon changes position on screen (in all cases except when X-axis rotation `= 0`). I need to be able to change the camera zoom AND keep the horizon unchanged (relatively to its 2D position). AFAIK, this can only be achieved by changing the X-axis rotation accordingly.

In the case of a well-defined symmetric view frustum, the solution can be found with some simple geometry:

I assume `YY` is in normalized device coordinates (ranging from -1 to 1). If they are in pixel coordinates, you have to convert them.

I have chosen the projection plane to be 1 unit far away from the camera. But any other distance would work as well. Then, the distance `y'` is simply

``````y' = YY * H/2
``````

`H/2` is the half screen height and can be calculated with:

``````H/2 = tan (fovy/2)
``````

where `fovy` is the camera's field of view in the vertical direction.

You want to find angle `alpha` for your rotation. This is simply:

``````tan alpha = y' / 1 = YY * tan(fovy / 2)
alpha = atan(YY * tan(fovy / 2)
``````

Be aware of the direction. Positive values specify downward rotations.

For arbitrary projections, this problem can be solved analytically:

Assuming we have projection matrix `P` and view matrix `V`, we want to solve for:

``````w-clip(P * V * (0 0 1 0)^T) = (... YY ...)
``````

Since you only want to allow translations and rotations about the x-axis for your camera, `V` has the form:

``````    / 1  0           0          tx \
V = | 0  cos alpha  -sin alpha  ty |
| 0  sin alpha   cos alpha  tz |
\ 0  0           0          1  /
``````

This yields the equation:

``````YY = (p23 * cos alpha - p22 * sin alpha) / (p43 * cos alpha - p42 * sin alpha)
``````

where `pij` is the entry of `P` in the `i`-th row and `j`-th column.

Use your favorite symbolic solver to get a solution for `alpha` and you can re-calculate your view-matrix.

# Related:

## How to calculate a random point inside a cube

math,vector,3d,cube
I'm trying to figure out the math to find a random point inside a cube. I have something small but it can't take into account the rotation of the cube. Here are some images of my results. Here you can see the cube is rotated to some degree but when...

## generating a pseudo unique number(code) based on a sequence of numbers with no repetition within 4 digits

php,math,hash
I'm generating a somewhat unique code that I don't want to repeat within at least 1000 consecutive numbers this is my function. take a number multiply it by another number then take the last \$length=5 digits before the decimal point function createPseudoUniqueString(\$input,\$length) { return substr( intval(\$input*738510.93067),-\$length) ; } is there...

## How can we use cordic to tanh(x+1)/tanh(x)?

math,cordic
How can we use cordic to tanh(x+1)/tanh(x) I can't get a idea about how to apply cordic to above function. In other word, which point on the above function, can we apply cordic?...

## sum of series AP GP clrs appendix A.1-4

algorithm,math,infinite,clrs,gp
I am trying to prove an equation given in the CLRS exercise book. The equation is: Sigma k=0 to k=infinity (k-1)/2^k = 0 I solved the LHS but my answer is 1 whereas the RHS should be 0 Following is my solution: Let's say S = k/2^k = 1/2 +...

## calculate % change in javascript

javascript,math,percentage
var current = 12000; var june = 14600; var may = 11200; I want percent change with respect to 'current' month parameter. The output should be in percent and it can add or subtract w.r.t. the current month. How to do this?...

## Excel log equivilent to JS Math.log()

javascript,excel,math
In my javascript code I have the following Math.log(20) = 2.995732273553991 In my excel formula I try to replicate this with =LOG(20) = 1.301029996 Anyone have any idea why I'm getting two different outcomes? I'd like my excel to match my js. ...

## Issues With length() And Multiples Of 3

java,math,multiplication,string-length
Lets see if someone can wrap their head around this: if you get the length of a String using length(), the String always being multiples of 3,(in my case: "1.02.03.04.05.06.07.0 etc.") each 3 characters representing a letter, with .1 indicating a capitol letter, how do you use the length to...

## Convert a large int to a float between 0.0f and 1.0f

math,unity3d,numbers
I'm working on a project where I have a pane that goes from 0 to 500 on the x and z, but I need to convert that coordinate (from 0 to 500) to a float in the range of 0.0 to 1.0 in because it is required from some of...

## Displaying surface with non-rectangular boundary

matlab,image-processing,plot,3d,surface
Refer to the attached image. I want to display the image in Matlab using the function surf(). However, I only want to display the region of the actual object without the background (the pale-green region surrounding the actual object which has 0 value). How to do that? I tried replacing...

## XSLT remove data if attribute value is higher than threshold

xml,xslt,math,operator-keyword,relational
I am newbie to XSLT. My am is to remove elements from XML based on a field if it is higher than some threshold value which I will be getting as parameter to it. My XML is as shown below: <tns:PM objectClass="MyNode" objectName="" className="com.project.converter.PMFamilyConverter" interfaceName="ComponentPM" xmlns:tns="http://www.myproject/SS/PMSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.myProject.com/SS/PM.xsd"> <tns:family desc="family0"...

## Determining angles on an SVG path between two lines

javascript,math,svg
I'm trying to make my robot follow a path that is a drawn SVG path on a webpage. To keep it simple, assume that the SVG Is like so: <svg xmlns="http://www.w3.org/2000/svg"> <path id="path" d="M 100 100 L 300 300 L 100 500 z"> </svg> Which makes a triangle. The robot...

## How to get a pizza program to round to a full pizza In PYTHON [duplicate]

python,math,rounding
This question already has an answer here: How do you round UP a number in Python? 9 answers Python round up integer to next hundred 6 answers so i'm making a pizza program in Python 3.3 that takes input from the user and prints the amount of pizza's needed....

## Implement pow(x, n)

c++,algorithm,math
Why does this function give the wrong answer -1 rather than the right answer 1 when I try this? myPow(-1.00000, -2147483648) double QuickPower(double x, int n) { if(n==0){ return 1; } if(n==1){ return x; } if(n>=2){ int res=n%2; double half=QuickPower(x,n/2); return res? Half*half*x: half*half; } } double myPow(double x, int...

## Arithmetic on a struct representing a large integer

c,math,largenumber,integer-arithmetic
I've wrote an implementation of Murmur3 hash and have defined the 128-bit keys as hash128_t typedef struct { uint64_t p1; uint64_t p2; } hash128_t; I'm trying to write my own hashmap using these keys, but I'm not exactly sure how to do arithmetic with a struct nor a number this...

## Points, Vectors, Dot Product & Cross Product of python [on hold]

python,math
Need help with this of python Write a program with 3 functions to find out the (function 1) dot product, (function 2) angle, and (function 3) cross product of two vectors. Program should ask a user to input three points in 3D space such as (x1, y1, z1), (x2, y2,...

## randint() unexpected behavior

python,math
I'm writing a python script to find out some things about randomization. I have the following code: from random import randint one = 0 two = 0 olddiff = 0 diff = 0 sumdiff = 0 avgdiff = 0 headcounter = 0 counter = 0 while (headcounter < 500000): while...

## How can I correctly convert geographical coordinates to pixels on screen?

java,math,2d,map-projections,mercator
I'm trying to make a Java project that pinpoints the place on a image of a map, when given coordinates (taken from Google Maps). I've tried using the top-left corner of the image (place that has highest latitude, and the lowest longitude), as an some kind of an reference point,...

## JavaFX 8 Transform to pitch, yaw and roll rotation angles

matrix,3d,rotation,javafx-8
Implementing the answer from this thread I have this code that translates the deltayaw, deltaroll and deltapitch angles into one angle and rotates a node around it. The angles that are taken as the parameters are momentary changes of angles since giving the whole angles would ignore the changes in...

## Why we use CORDIC gain?

math,fpga,cordic
I'm studying the cordic. And I found the cordic gain. K=0.607XXX. From CORDIC, K_i = cos(tan^-1(2^i)). As I know the K is approched 0.607xxx.when I is going to infinity this value come up with from all K multiplying. I understand the reason of exist each k. But I am curioused...

## Power by squaring for negative exponents

c,algorithm,math,recursion
I am not sure if power by squaring takes care of negative exponent. I implemented the following code which works for only positive numbers. #include <stdio.h> int powe(int x, int exp) { if (x == 0) return 1; if (x == 1) return x; if (x&1) return powe(x*x, exp/2); else...

## MATLAB: How to make camera light follow 3D Rotation

matlab,user-interface,3d,rotation,lighting
I have come into a problem recently when trying to rotate 3D objects. I am building a GUI and I have a separate figure where an object is plotted. In the figure, I allow the user to use MATLAB's built-in rotate button to move the object around. However, I am...

## Separating axis theorem: rotation around center of mass

c++,math,rotation,rotational-matrices,separating-axis-theorem
The problem is in Polygon::FindAxisLeastPenetration: double Polygon::FindAxisLeastPenetration(unsigned int *faceIndex, const Polygon &polygonA, const Polygon &polygonB) const { double bestDistance = -std::numeric_limits<double>::infinity(); unsigned int bestIndex; for (unsigned int i = 0; i < polygonA.points.size(); i++) { Vector2D n = polygonA.normals[i]; Vector2D nw = polygonA.rotationMatrix * n; //ROTATION Matrix22 buT = polygonB.rotationMatrix.Transposed();...

## why when i change slaying in the last else statment it crashes the browser

javascript,arrays,math
i was playing around in code academy and for some reason when i change the last slaying i the else statement to true instead of making the person invincible it just crashes my browser any ideas why thank you in advance var slaying = true; var youHit = Math.floor(Math.random()*2); var...

## How to calculate the number of all possible combinations for a range of numbers from 1 to N?

python,math,combinations,itertools
Other than doing this: from itertools import combinations def brute_force(x): for l in range (1,len(x)+1): for f in list(combinations(range(0,len(x)),l)): yield f x = range(1,18) len(list(brute_force(x))) [out]: 131071 How could I mathematically calculate the number of all possible combinations? Is there a way to do it computationally without enumerating the possible...

## creating a 3d array in python does not create array correctly

python,arrays,3d
I wish to make a 3d array by appending each element to the array in a loop, but instead of getting a 3d array I just get a 1d array with more elements in it. I'm working in Python 2.7 I want to get labels = [1,34,u'te],[2,44,u've],[4,43,u'ht] My code so...

## 3 X 3 magic square recursively

c++,algorithm,math,recursion
I'm trying to find all possible solutions to the 3X3 magic square. There should be exactly 8 solutions. My code gets them all but there are a lot of repeats. I'm having a hard time tracking the recursive steps to see why I'm getting all the repeats. // This program...

## c++ mathematical calculations [closed]

c++,loops,math
I have to solve problem using c++, and can't really come up with solution. The condition is. In a skyscraper there are a lot of offices, on every office door there must be put plate with its number from 1 to the number of the last office. The plate can...

## LinkedHashSet and subList, getting n of collection

I am trying to do a homework in math which is find a subset of collection {1,2,..,n} where n is a number given in the code, I cannot get it done with the sublist so I need to get your help with a math programming. For example for n =...

## Rotate line segment with Button

math,javafx,geometry,coordinates
I have a line which has points (x1,y1) and (x2,y2). I wanted to attach a Button to it, it should align with the line by rotating based on the line segment points. I need some help in calculating the rotation angle for the Button.

## How to get rid of scale factor from CORDIC

math,vhdl,fpga,rtl,cordic
From CORDIC, K_i = cos(tan^-1(2^i)). As I know the K is approached 0.607xxx. How do I approach to 0.607xxx? Also does it mean that I can use 0.607xxx instead of cos(tan^-1(2^I))? I am citing from this article. I am trying to implement hyperbolic tanh function. And so far I understand...

## 2D Line reflection on a “mirror”

math,lua,love2d
So I've been working at this on and off for a week, googling and all and I haven't found how to do this. I have a table of "rays" and a table of "lines", and I want the lines to act as mirrors and reflect a ray whenever the ray...

## Rotate a grid of points in C++

c++,math,rotation,grid,geometry
If i had an array of point structs defined as struct Point{ float x; float y; }; How would I rotate the points in this array by a given angle? As an example: Any help would be appreciated!...

## Reverse ^ operator for decryption

c,algorithm,security,math,encryption
I'm trying to reverse the following code in order to provide a function which takes the buffer and decrypts it. void crypt_buffer(unsigned char *buffer, size_t size, char *key) { size_t i; int j; j = 0; for(i = 0; i < size; i++) { if(j >= KEY_SIZE) j = 0;...

## Visually midway between two points on a x axis log scale [closed]

matlab,math
The follow plot shows my question: I would like to add a line between the points of 1e-1 and 1e-2. So I thought just (1e-1+1e-2)/2. But for a log scale that is not "midway". How can I calculate the "visual" midway value between these, or any two points in this...

## Pygame 3D: How to and is it possible?

python,performance,dictionary,3d,pygame
So I want to program a 3d-mesh viewer in pygame, no other 3rd-party modules wanted (panda3d is perfect, but it's very complicated). My idea is: list_of_surfs = {'85 50':'70deg'} #the 85 50 and the dimensions of the #side/surf, and 70 deg of rotation to be able to see it, then...

## Matlab: Writing to a file

arrays,matlab,loops,math,for-loop
I want to store mean_2....mean_8, std_2....std_8, and coVariance as separate columns in a file. Help Please? This Code calculates IntervalFeatures of Erp signal with 256 time frames and plots them Datapoints are store in A ___________HowTheCodeWorks________________________________________________ each 1x4 sliding sub-matrix is made a column for eg:- if A is...

## PHP math does not work in sql value ZERO

I am trying to math between value of 0 but in php code it's work properly but when that value come from database then it does not work. Here is a demo to test only PHP code: <?php \$a = 0; echo \$a+1; ?> The result of this code 1...

## 3D Lines disappear sometimes

javascript,d3.js,3d,three.js
I am building 3d line chart from data here is the code Demo the part of code that generate line is parentTransform = new THREE.Object3D(); var _color = d3.scale.category10(); for (var i = 5; i > 0; i--) { var material = new THREE.LineBasicMaterial({ // color: 0x0000ff,linewidth: 30 color: _color(i),...

## Solving a complex recurrence relation for the Traveling Salesman

algorithm,math,time-complexity,computer-science,recurrence-relation
I need to solve the exact time complexity for the brute force version of the Traveling Salesman using a recurrence relation. I've worked out the recurrence relation to be as follows: T(n)=T(n-1)*(n-1)+1 But I'm having trouble reducing that that to a closed form of the function, and thus get the...

## Calculate The object angle(face) having two points? [closed]

c++,math,geometry,angle
C++, I want calculate the angle of the direction of the two points. Here is a picture which shows the two points and the direction of how to get the angle of the direction? p1 - start point. p2 - direction point. me need direction angle(facing?) from p1 to p2...

## fixed point multiplication for normal multiplication

math,floating-point,fixed-point
I need to multiply X with a floating point number in floating point as i don't have floating point operations in my processor. I understand the method but don't know why that method exists? Suppose we want to multiply 2*4.5 in decimal I do the below: 2 * 4.5 (100.1)...

## Ranking with time weighting

python,algorithm,sorting,math
I am looking for a basic algorithm that gives more weigh to the recent reviews. So, the output value of the algorithm is mutable. For example, two reviews with exactly the same score, will have a different ranking based on the timestamp of the creation. Review_1 Score 10 creation 10/5/2014...

## Matlab: For loop with window array

arrays,matlab,math,for-loop,while-loop
This is my one dimensional array A, containing 10 numbers: A = [-8.92100000000000 10.6100000000000 1.33300000000000 ... -2.57400000000000 -4.52700000000000 9.63300000000000 ... 4.26200000000000 16.9580000000000 8.16900000000000 4.75100000000000]; I want the loop to go through like this; (calculating mean interval wise) - Interval length of 2,4,8 (a(1)+a(2))/2 - value stored in one block of...

## Bash script for basic mathematic operations

linux,bash,math
I am new to Linux, and I just want a Bash Script to do basic arithmetic operation to a text file 1 2 3 4 and the results should be addition and multiplication in separate text files say add + 5 for 1st text file and mult * 5 in...

## Removing a prior sample while using Welford's method for computing single pass variance

algorithm,math,statistics,variance,standard-deviation
I'm successfully using Welford's method to compute running variance and standard deviation as described many times on Stack Overflow and John D Cook's excellent blog post. However in the stream of samples, sometimes I encounter a "rollback", or "remove sample" order, meaning that a previous sample is no longer valid...

## implement pow in java without using math lib

java,math
I did a code like this: public static double myPow(double x, int n){ if(n==0) return 1; double t = myPow(x, n/2); if(n % 2 != 0){ if(n < 0){ return (1 / t * t * x); } else { return t * t * x; } } else {...

## What is the reason behind calculating GCD in Pollard rho integer factorisation?

algorithm,math,greatest-common-divisor,number-theory,clrs
This is the pseudo code for calculating integer factorisation took from CLRS. But what is the point in calculating GCD involved in Line 8 and the need for doubling k when i == k in Line 13.? Help please....

## Have I properly sorted these runtimes in order of growth?

math,big-o,time-complexity,asymptotic-complexity
I am doing this small task which I have to arrange asymptotic runtime in ascending order. Here are the runtimes: Here is the order I believe they should go in: log10(n^4), n^3, 2^((log4n)), 2^(100n), e^pi^4096, n! + 12^1000 Is this correct? Or are there any errors? Thanks!...

## Math operations within HTML

html,math
Is it possible to perform math operations within HTML, like <div width="50/2">? Or can this only be done using Javascript or CSS?