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.
Related
When using SUMSERIES I need to specify "the array or range containing the coefficients of the power series" but I want to make it so the number of elements is dynamic while the element itself (1) remains the same.
Example:
SUM FROM 0 TO N of x^1,5
(cell) Length of series N : 7 -- > SUMSERIES(1,5;0;1;{1,1,1,1,1,1,1})
But I should be able to change the seven for a 3 and get --> SUMSERIES(1,5;0;1;{1,1,1})
In Java for example you'd declare and instantiate the array --> int[] arr = new int[N];
And then fill in a loop --> for(int i = 0; i <arr.length; i++) {arr[i] = 1,5}
Thanks in advance and sorry if the explanation isn´t clear, it's my first time around hehe
this should work:
=SUMSERIES(1,5;0;1;SEQUENCE(1,[cell],1,0))
try:
=ARRAYFORMULA(SIGN(TRANSPOSE(ROW(INDIRECT("A1:A"&A1)))))
and then:
=INDEX(SUMSERIES(1,5; 0; 1; SIGN(TRANSPOSE(ROW(INDIRECT("A1:A"&A1))))))
In older version of Excel you can get this array using this (all of them are array formulas)
=INDEX(MUNIT(n),1,0)*0+x for horizontal array
=INDEX(MUNIT(n),0,1)*0+x for vertical array
Where:
n is dimension of the array
x is value of each item in the array
How it works:
MUNIT creates an identity matrix of size N
+---++---+---+---+---+---+
| || 1 | 2 | . | . | n |
+---++---+---+---+---+---+
+---++---+---+---+---+---+
| 1 || 1 | 0 | 0 | 0 | 0 |
| 2 || 0 | 1 | 0 | 0 | 0 |
| . || 0 | 0 | 1 | 0 | 0 |
| . || 0 | 0 | 0 | 1 | 0 |
| n || 0 | 0 | 0 | 0 | 1 |
+---++---+---+---+---+---+
Now we extract one (the first) row/column (n is set to 7 here)
=INDEX(MUNIT(7),1,0) for row extraction
=INDEX(MUNIT(7),0,1) for column extraction
And fill it with desired number (desired number here is 9 here)
=INDEX(MUNIT(7),1,0)*0+9 for row
=INDEX(MUNIT(7),0,1)*0+9 for column
What`s the easiest method to split a matrix in 4?
I have a nxn matrix, where n is multiple of 4;
http://i.stack.imgur.com/S4H2m.png
____________________
| | |
| | |
| 1st | 2nd |
| | |
|--------+----------
| | |
| 4th | 3rd |
| | |
|________|_________|
I don`t need to make a new matrix, only to get the ranges of i,j that refer to that new matrix;
1 st quadrant range of indices 0 - i/2 and 0 - j/2
2nd 0 - i/2 and j/2+1 - j
3rd i/2+1 - i and j/2+1 - j
4th i/2+1 - i and 0 - j/2
Maybe this could help you:
First matrix should go from: (0,0) - (n/2-1,n/2-1)
Second matrix should go from: (0,n/2) - (n/2-1,n-1)
3th matrix should go from: (n/2,n/2) - (n-1,n-1)
4th matrix should go from: (n/2,0) - (n-1,n/2-1)
How can i visualize 2D array with surface(mesh, surf) for incomplete dataset?
'Incomplete' means (v - known values, 0 - unknown):
1 | 2 | 3 | 4 | 5
1 | v | 0 | v | 0 | v
2 | 0 | 0 | 0 | 0 | 0
3 | v | 0 | v | 0 | v
4 | v | 0 | v | 0 | v
5 | 0 | 0 | 0 | 0 | 0
Such data indexing is handy for analyzing non-linear relation between variables.
The thing i want is working somehow with plot function. Lets say, x = [1,2,4,5]. plot will show continuous figure.
Is it possible to do so for 2D arrays without manual interpolation? Don't care about smoothness. Linear connection of known points is alright.
So you have non-linear sampling (x = [1 3 5], y = [1 3 4]), and you don't want to interpolate? I don't think surf etc will handle it. Sounds like a job for plot3.
This is mildly ugly (see result) but I'm presuming you just want to visualise it to get a feel for the data. First make up your x and y with repmat if you don't already have them like this:
x =
1 3 5
1 3 5
1 3 5
y =
1 1 1
3 3 3
4 4 4
Then you'll need your values without all the zeros in to match:
z =
6 8 10
6 5 4
4 2 1
This can be plotted with markers (might be the simplest if you have lots of points). Or you can use this trick to make a "mesh" out of two sets of lines:
plot3(x,y,z)
hold on
plot3(x',y',z')
xlabel('x');
ylabel('y');
Exactly as with your plot example, this simply linearly connects between the existing points.
You could replace the 0 values for NaN values.
Both, surf and mesh work with NaN.
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).
I want to assign weightings to a randomly generated number, with the weightings represented below.
0 | 1 | 2 | 3 | 4 | 5 | 6
─────────────────────────────────────────
X | X | X | X | X | X | X
X | X | X | X | X | X |
X | X | X | X | X | |
X | X | X | X | | |
X | X | X | | | |
X | X | | | | |
X | | | | | |
What's the most efficient way to do it?
#Kerrek's answer is good.
But if the histogram of weights is not all small integers, you need something more powerful:
Divide [0..1] into intervals sized with the weights. Here you need segments with relative size ratios 7:6:5:4:3:2:1. So the size of one interval unit is 1/(7+6+5+4+3+2+1)=1/28, and the sizes of the intervals are 7/28, 6/28, ... 1/28.
These comprise a probability distribution because they sum to 1.
Now find the cumulative distribution:
P x
7/28 => 0
13/28 => 1
18/28 => 2
22/28 => 3
25/28 => 4
27/28 => 5
28/28 => 6
Now generate a random r number in [0..1] and look it up in this table by finding the smallest x such that r <= P(x). This is the random value you want.
The table lookup can be done with binary search, which is a good idea when the histogram has many bins.
Note you are effectively constructing the inverse cumulative density function, so this is sometimes called the method of inverse transforms.
If your array is small, just pick a uniform random index into the following array:
int a[] = {0,0,0,0,0,0,0, 1,1,1,1,1,1, 2,2,2,2,2, 3,3,3,3, 4,4,4, 5,5, 6};
If you want to generate the distribution at runtime, use std::discrete_distribution.
To get the distribution you want, first you basically add up the count of X's you wrote in there. You can do it like this (my C is super rusty, so treat this as pseudocode)
int num_cols = 7; // for your example
int max;
if (num_cols % 2 == 0) // even
{
max = (num_cols+1) * (num_cols/2);
}
else // odd
{
max = (num_cols+1) * (num_cols/2) + ((num_cols+1)/2);
}
Then you need to randomly select an integer between 1 and max inclusive.
So if your random integer is r the last step is to find which column holds the r'th X. Something like this should work:
for(int i=0;i<num_cols;i++)
{
r -= (num_cols-i);
if (r < 1) return i;
}