math,vector,3d,cube , How to calculate a random point inside a cube


How to calculate a random point inside a cube

Question:

Tag: 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.

enter image description here

enter image description here

Here you can see the cube is rotated to some degree but when I generate some points it retains the shape as if the cube was normal (I think the term is called axis aligned but I'm not sure).

I'm using a Vector to represent the extent of the cube but for the life of me I can't figure out how to get the points to follow it when it's rotated.

Can someone point me in the right direction as to how I would do this?

EDIT1:

enter image description here

Now its misaligned and it goes even weirder when I rotate it sideways.

Can someone walk me through it from the beginning? I think my base line math is all wrong to begin with.


Answer:

Generate the points in the straight position then apply the rotation (also check the origin of the coordinates).


Related:


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...

Is it bad (or even dangerous) to random_shuffle vector of shared_ptrs?


c++,vector,shared-ptr,smart-pointers,shuffle
using namespace std; vector< shared_ptr<MyObject> > objects; // objects gets filled in by something random_shuffle(objects.begin(), objects.end()); Is this bad or inefficient for any reason? Is there a better way of doing it? (I want my array of smart pointers sorted randomly) EDIT: I'm asking because there are going to be...

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...

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....

R Program Vector, record Column Percent


r,vector,percentage
This is my vector head(sep) I must find percent of all SEP 11 in each row. For instance, in first row, percent of SEP 11 is 100 * ((63 + 124)/ (63 + 124 + 0 + 0)) And would like this stored in newly created 8th column Thanks dput...

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...

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;...

TypeError: zip argument #1 must support iteration (Vector sum for Ipython)


python,vector,ipython,linear-algebra,ipython-notebook
Hi I'm following "Data Science from Scratch" and I got an error when I was making vector sum function. Could someone help? The code: a = [1,2,3] b = [2,3,4] def vector_add(v, w): """adds two vectors componentwise""" return [v_i + w_i for v_i, w_i in zip(v,w)] vector_add(a,b) #prints [3, 5,...

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...

Add a matrix of 2x2 into a vector in c++


c++,matrix,vector
I am trying to fill a vector with a matrix of values in c++. I'm not very self confident with this procedure (I don't know well about pointers and I don't know if I need it here) however I am trying this int auxMat[gray.rows][gray.cols]; vector<int> collectionSum; collectionSum.push_back(auxMat); When I try...

Who should clear a vector retrieved by reference?


c++,vector,pass-by-reference
Let's say I have this very simple code: std::vector<int> getVector( int size ) { std::vector<int> res; for ( size_t i = 0; i != size; ++i ) res.push_back( i* 23 ); return res; } int main () { std::vector<int> v; v = getVector(10); std::cout << "Size1 is " << v.size()...

Convert Vector to String?


java,vector
I have a problem, when I am converting vector to String. Error: Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.util.Vector cannot be cast to java.lang.String at Package_Sync.Package_sync.dataToDB(Package_sync.java:98) at Package_Sync.Package_sync.<init>(Package_sync.java:29) at Package_Sync.Package_sync$3.run(Package_sync.java:319) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756) at...

R: recursive function to give groups of consecutive numbers


r,if-statement,recursion,vector,integer
Given a sorted vector x: x <- c(1,2,4,6,7,10,11,12,15) I am trying to write a small function that will yield a similar sized vector y giving the last consecutive integer in order to group consecutive numbers. In my case it is (defining groups 2, 4, 7, 12 and 15): > y...

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...

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...

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();...

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!...

Reordering vector of vectors based on input vector


c++,c++11,vector,stl,idioms
In a small application, I've been using a std::vector of std::vector<std::string> to temporarily store some data (pulled from a non-SQL database) before processing it and uploading it to a SQL database. Unfortunately, the API from which I am extracting the data does not necessarily return fields in the order specified...

Sorting vector of Pointers of Custom Class


c++,sorting,c++11,vector
I have vector<FPGA*> current_generation_, which I'd like to sort by FPGA member fitness_ using the sort_members function. Applicable code follows: bool sort_members (FPGA* fpga_first, FPGA* fpga_second) { return (fpga_first->fitness() < fpga_second->fitness()); }; fpga.hpp #include <vector> class FPGA { public: explicit FPGA(int input_gates, int output_gates, int normal_gates); const int fitness(); protected:...

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...

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...

LinkedHashSet and subList, getting n of collection


java,math,set,linkedhashset
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 =...

Cannot Properly Print Contents of Vector


c++,arrays,string,vector,segmentation-fault
I have some C++ code where I'm taking input from a user, adding it to a vector splitting the string by delimiter, and for debugging purposes, printing the vector's contents. However, the program only prints the first location of the vector, and then none of the rest. main.cpp #include <cstdlib>...

How can I generate all the possible combinations of a vector


r,vector
I have a vector, say A,B,C,D,E and I am interested in generating all the possible combination of the vector elements. The desired output is given below. B-A,C-A,D-A,E-A,C-B,D-B,E-B,D-C,E-C,E-D...

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...

Sending vector data in the bus


vector,simulink,bus
I have a vector data (an array variable for example float32 mydata[5];). for transmitting a single primitve/basic data in a bus its pretty simple. inside_data=Simulink.BusElement; inside_data.Name='somename'; inside_data.SampleTime = -1; inside_data.datatype='single'; this element can be put inside a using Bus=Simulink.Bus; Bus.Elements=inside_data; But this works when the input is a primitive. But...

decltype does not resolve nested vectors. How can I use templates for nested vectors?


c++,templates,vector,nested
I am trying to overload the + operator to deal with nested vectors. I thought the function would call itself until the nested vectors resolve to basic types, but instead I get a huge list of errors when I compile it. My vector operations that I have defined work for...

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?...

Debug error: Vector iterator not dereferencable and Vector subscript out of range [closed]


c++,vector,stl,iterator
I am trying to implement Rateless codes, but I have some issues in decoding. In the first implementation I used iterator to loop over a vector of vector and do some operation (like erase()) but I was getting this debug error: Vector iterator not dereferencable. After some googling I found...

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...

OpenLayers 3: simple LineString example


javascript,vector,openlayers,openlayers-3
i'm new to OpenLayers and i am looking for some help drawing lines on a map, i've been trying various things from various different posts about drawing LineStrings but i can't get it to work! I just need to figure out how to draw a line between to coordinates. heres...

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 {...

how to sort this vector including pairs


c++,vector
I want to sort in ascending order according to the first element of the inner pair, i.e. a in this case. But its not at all sorting. I am not sure if my function func logic is correct. #include<iostream> #include<algorithm> #include<vector> using namespace std; bool func(const pair<int,pair<int,int> >&i , const...

reinterpret_cast vector of derived class to vector of base class


c++,vector,c++03
I have a 3rd-party class, say, class A, and a function accepting vector of class A from the same 3rd-party, say f3() (See simplified program below). For easier use of A, I created a derived class B. Many part of my program used class B. The question is, how can...

sorting vector of pointers by two parameters


c++,sorting,vector
I am trying to sort a vector of pointers to a class A using std::sort, but am having some difficulties. Imagine class A beings just a point, containing x and y coordinates. I want to sort the vector by y coordinates from biggest to lowest using some fixed offset value....

Have an if statement look ONLY at the first word in a string [duplicate]


c++,string,if-statement,vector
This question already has an answer here: how to check string start in C++ 9 answers I have a vector composed of strings, some strings have single words, some have multiple words, some have numbers, etc. I have a code that deletes elements of the vector IF the entire...

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...

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...

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...

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.

Create a Triangular Matrix from a Vector performing sequential operations


r,for-loop,matrix,vector,conditional
I have been trying to solve the following problem. Suppose I have the following vector: aux1<-c(0,0,0,4,5,0,7,0,0,10,11,12) where the numbers represent the number of the row. I want to calculate the distance between the differents elements of this vector fixing the first component, then the second and so on. If the...

Defining a “vector_cast” template function that works with a variable amount of nested vectors


c++,vector,casting,nested
I want to be able to do something like this for example. std::vector<std::vector<int>> e = {{ 1 },{ 2 }}; std::vector<std::vector<double>> q = {{1.5},{2.5}}; q = vector_cast<double>(e); But also have vector_cast be able to handle any number of nested vectors. Here is my attempt at this. The code works if...

vector::push_back ( A * ) creating leaks?


c++,vector,push-back
I'm trying to create a really simple virtual filesystem (represented as a tree)(for my FTP server) mapped to several places on my real one. Nodes are represented by a Node object which holds pointers to its contents in a vector. I know that for every new there is a delete...

create vector of objects on the stack ? (c++)


c++,vector,heap-memory
I am creating a temporary vector of pointers to myObject objects. But I am wondering about what happens to the objects I created... { std::vector<myObject *> myVector; myVector.reserve(5); for (int i = 0 ; i < 5 ; ++i){ myVector[i] = new myObject(); } } I assume that at the...

C language, vector of struct, miss something?


c,vector,struct
This is a part of my program that I want to create a vector of struct typedef struct { char nome[501]; int qtd; int linha; int coluna; } tPeca; tPeca* criarPecas(FILE *pFile, int tam) { int i; tPeca *pecaJogo = (tPeca*)malloc(tam*sizeof(tPeca)); if (pecaJogo == NULL) return NULL; for (i =...

Add same value multiple times to std::vector (repeat)


c++,vector,std
I want to add a value multiple times to an std::vector. E.g. add the interger value 1 five times to the vector: std::vector<int> vec; vec.add(1, 5); vec should be of the form {1,1,1,1,1} afterwards. Is there a clean c++ way to do so?...

Vectorize thinking


r,vector,vectorization
I'm having a vectorization problem. Say I'm having a vector x <- c(0,0,1,0,1,1,0) , what I want is to get the vector (1,0,1,1), or to remove all the 0s from the first spot to the first non-zero spot and all the 0s from last non-zero spot to the last spot....

program failed. sharedPtr vector c++


c++,vector
PLEASE, i realy need help.. don't panic from the sharedPtr implementation I'm having a problem with my project in c++. In my project, I'm creating a company which holds all the information about its employees (artist, programmer,manager), projects ,... for holding all the employees I use a vector of SharedPtr(use...