I don't know how to setup distance
where I should stand to look at my 2d stuff(which at center there is a ball pos:1024/2,768/2)
I use gluLookAt and glPerspective to give my 2d rotated object more 3d feel
anyway here is the code I use with glOrtho:
glMatrixMode ( GL_PROJECTION );
glLoadIdentity();
glOrthof ( 0, 1024, 768, 0, 0, 1000.0f );
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity();
and this is when I try to setup with glPerspective and gluLookAt:
glMatrixMode ( GL_PROJECTION );
glLoadIdentity();
gluPerspective(90,1024/768,0,300);
gluLookAt(1024 * 0.5,768 * 0.5f,-????, 1024 * 0.5,768 * 0.5,0, 0,-1,0);
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity();
Basically I just want those codes that works the same,I am not sure how to setup the fovy value of gluPerspective,and the ??? from gluLookAt,how to project the full size with width 1024,and height 768?
Well glOrtho is supposed to yield a parallel projection, so essentially using gluPerspective is going exactly the other way. If you're hoping to find a special case of gluPerspective that acts like glOrtho, the problem is that the matrices they generate are different in some ways you can't reach - note the bottom right corner, in particular, in what they generate:
glOrtho
| 2 |
|---------- 0 0 t |
|right-left x |
| |
| 2 |
| 0 ---------- 0 t |
| top-bottom y |
| |
| |
| 0 0 -2 |
| -------- t |
| far-near z |
| |
| 0 0 0 1 |
gluPerspective
| f |
| ------ 0 0 0 |
| aspect |
| |
| 0 f 0 0 |
| |
| zFar+zNear 2*zFar*zNear |
| 0 0 ---------- ------------ |
| zNear-zFar zNear-zFar |
| |
| 0 0 -1 0 |
So it's going to be hard to set the 2/(top-bottom) and the bottom row correctly, for starters.
If this line is the core of your issue:
gluLookAt(1024 * 0.5,768 * 0.5f,-????, 1024 * 0.5,768 * 0.5,0, 0,-1,0);
...then just set thee -???? to a positive value indicating the distance to your eye from the center of the scene (OpenGL's positive Z points towards the viewer).
Related
For each row in a table, I want to find the minimum value across a couple of numeric columns, then take the name of that column (which holds the desired value) and populate a new column with the name (or custom string).
A few rules first in my specific scenario: the value to be found across the columns must also be > 0. Also, if no value in the column is > 0, then a custom string should be placed (ie. 'none').
For example, take this table below with columns alpha to delta storing the values:
id | alpha | bravo | charlie | delta
------+--------+--------+---------+--------
1 | 5 | 2.3 | -1 | -5
2 | 9 | 8 | 3 | 1
3 | -1 | -4 | -7 | -9
4 | 6.1 | 4 | 3.9 | 0
for each row, I want to find out which column holds the lowest positive value. My expected output is something like this:
id | alpha | bravo | charlie | delta | lowest_postive
------+--------+--------+---------+--------+---------------
1 | 5 | 2.3 | -1 | -5 | 'col: bravo'
2 | 9 | 8 | 3 | 1 | 'col: delta'
3 | -1 | -4 | -7 | -9 | 'col: none'
4 | 6.1 | 4 | 3.9 | 0 | 'col: charlie'
Should I use a CASE ... WHEN ... THEN ...? Should I be converting the row into an array first, then assinging each position in the array?
You can do:
select *,
case when mp = alpha then 'col: alpha'
when mp = bravo then 'col: bravo'
when mp = charlie then 'col: charlie'
when mp = delta then 'col: delta'
end as lower_positive
from (
select *,
least(
case when alpha > 0 then alpha end,
case when bravo > 0 then bravo end,
case when charlie > 0 then charlie end,
case when delta > 0 then delta end
) as mp
from t
) x
However, this solution doesn't account for multiple minimums; the first one (from left ro right) wins.
#include <conio.h>
#include <math.h>
#include <graphics.h>
#include <dos.h>
int main() {
int gd = DETECT, gm;
int angle = 0;
double x, y;
initgraph(&gd, &gm, "C:\\TC\\BGI");
line(0, getmaxy() / 2, getmaxx(), getmaxy() / 2);
/* generate a sine wave */
for(x = 0; x < getmaxx(); x+=3) {
/* calculate y value given x */
y = 50*sin(angle*3.141/180);
y = getmaxy()/2 - y;
/* color a pixel at the given position */
putpixel(x, y, 15);
delay(100);
/* increment angle */
angle+=5;
}
getch();
/* deallocate memory allocated for graphics screen */
closegraph();
return 0;
}
This is the program. Why are we incrementing the angle and how this angle is relevant to graph? I changed the value of angle to 0 and the wave became a straight line. I want to know what is happening with this increment.
Why are we incrementing the angle and how this angle is relevant to graph
The sine function takes an angle as argument, typically in radiant. The program implements the angle in degrees, so it's getting scaled to radiant the moment is gets passed to sin().
The sine function is periodical to (repeats itself after) 2*pi or 360 degrees:
+---------+---------+------------+
| angle | sin(angle) |
+---------+---------+ |
| Radiant | Degrees | |
+---------+---------+------------+
| 0 | 0 | 0 |
+---------+---------+------------+
| 1/2*pi | 90 | 1 |
+---------+---------+------------+
| pi | 180 | 0 |
+---------+---------+------------+
| 3/2*pi | 270 | -1 |
+---------+---------+------------+
| 2*pi | 360 | 0 |
+---------+---------+------------+
| 5/2*pi | 450 | 1 |
+---------+---------+------------+
| 3*pi | 540 | 0 |
+---------+---------+------------+
| 7/2*pi | 630 | -1 |
+---------+---------+------------+
| 4*pi | 720 | 0 |
+---------+---------+------------+
| ... | ... | ... |
and so on ...
changed the value of angle to 0 and the wave became a straight line
The result of sin(0) is 0.
For the mathematical derivation you might like to have a look here.
I've been trying to learn about minimum span trees and the algorithms associated with it, namely Prim's, Kruskal's and Dijkstra's algorithms.
I understand how these algorithms work and have seen them in action but there is only one thing I don't understand about Prim's algorithm, which is an array that I don't understand what it is it's intention and how does it work.
So here is the situation:
I have to do an exercise wherein I am given an adjacency table and I have to run Prim's algorithm to create a minimum span tree.
The table looks like this:
0 |1|2| 3| 4| 5|
0| 0 73 4 64 40 74
1| 73 0 46 26 30 70
2| 4 46 0 77 86 14
3| 64 26 77 0 20 85
4| 40 30 86 20 0 22
5| 74 70 14 85 22 0
The numbers separated by the "|" are the vertices and the numbers in the table are the edges. Simple, I run the algorithm ( in this website for example: http://www.jakebakermaths.org.uk/maths/primsalgorithmsolverv10.html ) or just jot it down on paper and and draw the minimum span tree and I get the tree with the minimal cost of 86 and the edges that have been used are 4, 26, 20, 22 and 14.
Now here comes the problem, apparently just solving it wasn't enough. I need to find the values of an array called closest[0,...,5]. I know it is used in the algorithm but I don't know it's purpose and what I should do with it or how to get it's values.
I have searched the internet for it and found this link about Prim's algorithm:
http://lcm.csa.iisc.ernet.in/dsa/node183.html
Which defines the array "closest" as "For i in V - U, closest[i] gives the vertex in U that is closest to i".
I still don't understand what it is, what it is used for and what the values inside of them are.
All I know the answer to my exercise is
closest[1] = 3
closest[2] = 0
closest[3] = 4
closest[4] = 5
closest[5] = 2
Thank you in advance.
When doing a MST with Prim's algorithm, it is important to keep track of four things: the vertex, has it been visited, minimal distance to vertex, and what precedes this vertex (this is what you are looking for).
You start at vertex 0, and you see that the closest vertex to 0 is 2. At the same time, you could have visited all other nodes, but with bigger distances. Nevertheless, the closest node to 0 is 2, so thus 2 becomes visited and its parent is set to vertex 0. All the other nodes are not visited yet, but its parent as of now is set to 0, with its respective distance. You now need to set the smallest distance vertex to visited, and now consider this node as the node to be considered.
Vertex | Visited | Distance | Parent
0 | T | - | -
1 | F | 73 | 0
2 | T | 4 | 0
3 | F | 64 | 0
4 | F | 40 | 0
5 | F | 74 | 0
We then check all the distances of nodes from 2. We compare the new distances from 2 to the other nodes to the distance from the other nodes from its previous distance, and if it needs to be updated, it gets updated. We now see that the distance from 2 to 5 is shorter than 0 to 5, and vertex 5 now becomes becomes visited, with its parent now equal to vertex 2.
Vertex | Visited | Distance | Parent
0 | T | - | -
1 | F | 46 | 2
2 | T | 4 | 0
3 | F | 64 | 0
4 | F | 40 | 0
5 | T | 14 | 2
Now we visit 5. One thing to note is that if a node is visited, we do not consider it in our distance calculations. I have simulated the rest, and hopefully you can see how you get the answer you're looking for.
Vertex | Visited | Distance | Parent
0 | T | - | -
1 | F | 46 | 2
2 | T | 4 | 0
3 | F | 64 | 0
4 | T | 22 | 5
5 | T | 14 | 2
Now visit 4
Vertex | Visited | Distance | Parent
0 | T | - | -
1 | F | 46 | 2
2 | T | 4 | 0
3 | T | 20 | 4
4 | T | 22 | 5
5 | T | 14 | 2
And now visit 3
Vertex | Visited | Distance | Parent
0 | T | - | -
1 | T | 26 | 3
2 | T | 4 | 0
3 | T | 20 | 4
4 | T | 22 | 5
5 | T | 14 | 2
I saw some code like this
if( ((c1^c2) & ~32)==0 )
{
...
}
In this snippet the code likely mean that if the if statement is true, then c1 and c2 are the same character in different case, meaning that one of those is +32 or -32 away from the other. Why is that?
I did test myself and discover that in some case it is true while in others not:
printf("%d", (65^97)& ~32); //output is 0. right
printf("%d", (97^65)& ~32); //output is 0. right
printf("%d", (50^82)& ~32); //output is 64!! not the same though 82-50=32
Why is that? what is the magic in it?
(c1^c2) & ~32) xors c1 and c2, the result contains the bits that are in both characters and & with ~32 clears (ignores) the bit 5. (It is zeroed whether it was same in both or not). Comparing this with zero, checks if all the bits other than bit 5 are same.
This can be used to check if 2 letters are equal ignoring their case in ascii representation if you are sure that atleast c1 or c2 is a valid latin character(a-z, A-Z).
To understand this, let's pick 2 characters with different case and compare them:
+---+---+---+---+---+---+---+---+
a | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 1 |
+---+---+---+---+---+---+---+---+
| x | | | | | |
+---+---+---+---+---+---+---+---+
A | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 1 |
+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+
a ^ A | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+
32 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+
~32 | 1 | 1 | 0 | 1 | 1 | 1 | 1 | 1 |
+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+
& | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+---+
You can try the same with j v/s J or t v/s z.
So there is no magic involved, only this logic.
Sometimes this condition is also written as:
if (ch1 == ch2 || (ch1 ^ 32) == ch2)
{
...
}
Suppose I have a cube as
P1(0, 0, 0) P5(0, 0, 1)
P2(1, 0, 0) P6(1, 0, 1)
P3(0, 1, 0) P7(0, 1, 1)
P4(1, 1, 0) P8(1, 1, 1)
Now I need to apply transformation/rotation/scale matrices. Say,
transform = Pt(3, 3, 5)
rotation = 30ยบ
scale = 2x`
Ok. But, where do I put each of these values into the matrices in order to get the final result? That confuses me alot.
edit
Lets say, for the P2, I have:
| 1 | | a b c |
| 0 | x | d e f | = R
| 0 | | g h i |
But what do I have in a,b,c,d,...i ?
To do it with a single operation you need a 4x4 matrix. Look at http://www.engineering.uiowa.edu/~ie_246/Lecture/OpenGLMatrices.ppt for some details and examples.
In the end you chain the transformations like this
point[i] = T1*T2*T3*..*vertex[i]
Each of the 8 points on the corners of the cube are a 3x1 vector. Your matrix transformations are 3x3 matricies.
Rotation about what axis? That will change what that rotation matrix will look like. Here's what it is about the x-axis:
| +cos(theta) -sin(theta) 0 |
Rx = | +sin(theta) +cos(theta) 0 |
| 0 0 1 |
The scale is easy: Multiply all the x-coordinates by a factor of two.
| 2 0 0 |
S = | 0 1 0 |
| 0 0 1 |
Apply these to each of your points.