How do I generate a vector/array of random cuboids without them intersecting? - math

Suppose I have constraints like the minimum and maximum dimensions of a random cuboid as well as constraints on its center's position. How would I generate a vector/array of cuboids in 3d space where they don't intersect with each other, without relying on checking every cuboid with each other and leave it to a random cuboid to be valid before pushing to the vector/array.
I've tried brute-forcing it by generating random cuboids and checking it against each of the other cuboids and only adding if it doesn't intersect, but I don't want to rely on just using random and hoping that it is valid.
Here are my implementations of a point intersecting with a cuboid, cuboid-cuboid intersection, and cuboid-cuboid[] intersection:
bool cuboid::point_intersects(Vec3 a, Cuboid b) {
Vec3 dimensions = {b.width, b.height, b.depth};
for (unsigned int i = 0; i < 3; i++) { // x,y,z
if (!(a[i] <= b.center[i] + dimensions[i] &&
a[i] >= b.center[i] - dimensions[i])) {
return false;
}
}
return true;
}
bool cuboid::intersects(Cuboid a, Cuboid b) {
Vec3 a_points[8];
Vec3 b_points[8];
cuboid::assign_vertices(a, a_points);
cuboid::assign_vertices(b, b_points);
for (unsigned int i = 0; i < 8; i++) {
if (cuboid::point_intersects(a_points[i], b)) {
return true;
}
if (cuboid::point_intersects(b_points[i], a)) {
return true;
}
}
return false;
}
bool cuboid::intersects_with_vector(std::vector<Cuboid> cuboids, Cuboid a) {
for (Cuboid c : cuboids) {
if (cuboid::intersects(a, c))
return true;
}
return false;
}
Here's how it is used:
#include "cuboid.h"
#include "vector3.h"
// #include <cstdlib>
#include <ctime>
#include <iostream>
#include <random>
#include <vector>
#define MIN_WIDTH 0.01
#define MAX_WIDTH 0.2
#define MIN_HEIGHT 0.01
#define MAX_HEIGHT 0.2
#define MIN_DEPTH 0.01
#define MAX_DEPTH 0.2
#define NUM_ROOMS 12
int main() {
std::mt19937 mt(time(nullptr));
std::vector<Cuboid> rooms = {};
rooms.push_back(cuboid::random_cuboid(MIN_WIDTH, MAX_WIDTH, MIN_HEIGHT,
MAX_HEIGHT, MIN_DEPTH, MAX_DEPTH, &mt));
for (unsigned int i = 0; i < NUM_ROOMS - 1; i++) {
Cuboid c = cuboid::random_cuboid(MIN_WIDTH, MAX_WIDTH, MIN_HEIGHT,
MAX_HEIGHT, MIN_DEPTH, MAX_DEPTH, &mt);
while (cuboid::intersects_with_vector(rooms, c)) {
c = cuboid::random_cuboid(MIN_WIDTH, MAX_WIDTH, MIN_HEIGHT, MAX_HEIGHT,
MIN_DEPTH, MAX_DEPTH, &mt);
}
rooms.push_back(c);
}
for (Cuboid c : rooms) {
std::cout << c << std::endl;
}
return 0;
}
In the following I attempt to generate cuboids whose center's lies in (0,1), with minimum and maximum height, width, and depth.

Related

How do I plot E8 (Exceptional Lie Group order 8) in 2D?

For the last week or so I have been struggling to find a resource that will allow me to make something like the 2D petrie polygon diagrams in this article.
My main trouble is finding out what the rules are for the edge and node connections.
I.e. in this plot, is there a simple way to make the image from scratch (even if it not fully representative of the bigger theory behind it)?
Any help is massively appreciated!
K
Here is how I solved this problem!
e8
// to run
// clink -c Ex8
// ./Ex8
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "dislin.h"
// method to generate all permutations of a set with repeated elements:
the root system
float root_sys[240][8];
int count = 0;
/// checks elements in root system to see if they should be permuted
int shouldSwap(float base[], int start, int curr)
{
for (int i = start; i < curr; i++)
if (base[i] == base[curr])
return 0;
return 1;
}
/// performs permutations of root system
void permutations(float base[], int index, int n)
{
if (index >= n) {
for(int i = 0; i < n; i++){
root_sys[count][i] = base[i];
}
count++;
return;
}
for (int i = index; i < n; i++) {
int check = shouldSwap(base, index, i);
if (check) {
float temp_0 = base[index];
float temp_1 = base[i];
base[index] = temp_1;
base[i] = temp_0;
permutations(base, index + 1, n);
float temp_2 = base[index];
float temp_3 = base[i];
base[index] = temp_3;
base[i] = temp_2;
}
}
}
// function to list all distances from one node to others
float inner_product(float * vect_0, float * vect_1){
float sum = 0;
for(int i = 0; i < 8; i++){
sum = sum + ((vect_0[i] - vect_1[i]) * (vect_0[i] - vect_1[i]));
}return sum;
}
/// inner product funtion
float inner_product_plus(float * vect_0, float * vect_1){
float sum = 0;
for(int i = 0; i < 8; i++){
sum = sum + (vect_0[i] * vect_1[i]);
}return sum;
}
int main(void){
// base vector permutations of E8 root system
float base_sys[8][8] = {
{1,1,0,0,0,0,0,0},
{1,-1,0,0,0,0,0,0},
{-1,-1,0,0,0,0,0,0},
{0.5,0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5},
{0.5,0.5,0.5,0.5,-0.5,-0.5,-0.5,-0.5},
{0.5,0.5,0.5,0.5,0.5,0.5,-0.5,-0.5},
{0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5},
{-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5,-0.5}
};
//permute the base vectors
for(int i = 0; i < 8; i++){
permutations(base_sys[i],0,8);
}
//calculating distances between all roots, outputting correspondence matrix
int distance_matrix[240][240];
for(int i = 0; i < 240; i++){
int dist_m = 100;
for(int ii = 0; ii < 240; ii++){
float dist = inner_product(root_sys[i], root_sys[ii]);
if(dist == 2){ //connecting distance in E8
distance_matrix[i][ii] = 1;
}else{distance_matrix[i][ii] == 0;};
}
}
//use another program to calculate eigenvectors of root system . . . after some fiddling, these vectors appear
float re[8] = {0.438217070641, 0.205187681291,
0.36459828198, 0.0124511903657,
-0.0124511903657, -0.36459828198,
-0.205187681291, -0.67645247517};
float im[8] = {-0.118465163028, 0.404927414852,
0.581970822973, 0.264896157496,
0.501826483552, 0.345040496917,
0.167997088796, 0.118465163028};
//define co-ordinate system for relevent points
float rings_x[240];
float rings_y[240];
//decide on which points belong to the system
for(int i = 0; i < 240; i++){
float current_point[8];
for(int ii = 0; ii < 8; ii++){
current_point[ii] = root_sys[i][ii];
}
rings_x[i] = inner_product_plus(current_point, re);
rings_y[i] = inner_product_plus(current_point, im);
}
//graph the system using DISLIN library
scrmod("revers");
setpag("da4l");
metafl("cons");
disini();
graf(-1.2, 1.2, -1.2, 1.2, -1.2, 1.2, -1.2, 1);
// a connection appears depending on the previously calculated distance matrix
for(int i = 0; i < 240; i++){
for(int ii = 0; ii < 240; ii++){
int connect = distance_matrix[i][ii];
if(connect == 1){
rline(rings_x[i], rings_y[i], rings_x[ii], rings_y[ii]);
distance_matrix[ii][i] = 0;
}else{continue;}
}
}
// More DISLIN functions
titlin("E8", 1);
name("R-axis", "x");
name("I-axis", "y");
marker(21);
hsymbl(15);
qplsca(rings_x, rings_y, 240);
return 0;
}
Extra points to anyone who can explain how to rotate the 2d plot to create a 3-d animation of this object

Why am I getting a run-time error on school server but not on big servers like HackerEarth or HackerRank for this minimum spanning tree code?

I got a homework where I needed to wright a program to find MST of a graph. I tried running it on the school server but I get a run-time error. On big servers like HackerEarth or HackerRank, however, I got correct answers on all test cases. The boundary for the number of edges and vertices is 100000 and for the weight of an edge 10000. Vertices are labeled from 0 to n.
Here is my code:
#include <stdio.h>
#include <algorithm>
using namespace std;
class Edge
{
public:
int x, y;
long long w;
bool operator<(const Edge& next) const;
};
bool Edge::operator<(const Edge& next) const
{
return this->w < next.w;
}
class DisjointSet
{
public:
int parent, rank;
};
DisjointSet set[100100];
Edge edges[100100];
int findWithPathCompression(int x)
{
if(set[x].parent != x)
set[x].parent = findWithPathCompression(set[x].parent);
return set[x].parent;
}
bool unionByRank(int x1, int y1)
{
int x = findWithPathCompression(x1);
int y = findWithPathCompression(y1);
if(x == y)
return false;
if(set[x].rank > set[y].rank)
set[y].parent = x;
else if(set[y].rank > set[x].rank)
set[x].parent = y;
else
{
set[y].parent = x;
set[x].rank++;
}
return true;
}
int main()
{
int n, m, e = 0, c = 0;
long long r = 0;
scanf("%d %d",&n,&m);
for(int i = 0; i <= n; i++)
{
set[i].parent = i;
set[i].rank = 1;
}
for(int i = 0; i < m; i++)
{
scanf("%d %d %lld",&edges[i].x,&edges[i].y,&edges[i].w);
edges[i].x;
edges[i].y;
}
sort(edges,edges + m);
while(e != n - 1)
{
if(unionByRank(edges[c].x,edges[c].y))
{
r += edges[c].w;
e++;
}
c++;
}
printf("%lld\n",r);
}

OpenCL distance between strings

I have this C++ code from the web that computes for distance between two strings. Can someone help me convert this to OpenCL for parallelism? I'm having hard time learning OpenCL.
#include <stdio.h>
#include <math.h>
#include <string.h>
#define MIN(x,y) ((x) < (y) ? (x) : (y))
int main()
{
int d[100][100];
int i,j,m,n,temp,tracker;
char s[] = "Sanfoundry";
char t[] = "Education";
m = strlen(s);
n = strlen(t);
for(i=0;i<=m;i++)
d[0][i] = i;
for(j=0;j<=n;j++)
d[j][0] = j;
for (j=1;j<=m;j++)
{
for(i=1;i<=n;i++)
{
if(s[i-1] == t[j-1])
{
tracker = 0;
} else {
tracker = 1;
}
temp = MIN((d[i-1][j]+1),(d[i][j-1]+1));
d[i][j] = MIN(temp,(d[i-1][j-1]+tracker));
}
}
printf("the Levinstein distance is %d\n",d[n][m]);
return 0;
}
Specifically, what part of the code must be put in a kernel? Also, what memory objects needed to be created?
Thanks

I don't know why this code for MICEMAZE in SPOJ is giving WA?

This is my code for MICEMAZE. Write a program that, given a description of the maze and the time limit, predicts the number of mice that will exit the maze. Assume that there are no bottlenecks is the maze, i.e. that all cells have room for an arbitrary number of mice.
#include <iostream>
#include <cstring>
#include <climits>
using namespace std;
int n,e,t,m;
int d[101][101];
int dis[101];
bool vis[101];
void djk()
{
memset(vis,false,sizeof vis);
for(int i=0;i<n;i++)
{
dis[i]=300000000;
}
dis[e]=0;
for(int i=0;i<n;i++)
{
int mi = INT_MAX, u;
for (int v = 0; v < n; v++)
if (vis[v] == false && dis[v] <= mi)
mi = dis[v], u = v;
vis[u]=true;
for(int j=0;j<n;j++)
{
if(d[u][j]!=0 && dis[j]!=INT_MAX && vis[j]==false && dis[j]>dis[u]+d[u][j])
dis[j]=dis[u]+d[u][j];
}
}
}
int main()
{
cin>>n>>e>>t>>m;
memset(d,0,sizeof d);
e--;
for(int i=0;i<m;i++)
{
int x,y,s;
cin>>x>>y>>s;
d[x-1][y-1]=s;
}
int cnt=0;
djk();
for(int i=0;i<n;i++)
{
//cout<<dis[i]<<endl;
if(dis[i]<=t) cnt++;
}
cout<<cnt<<endl;
return 0;
}

Facing incorrect output while linking programs

I am having a trouble in calling the user defined functions in the main program, using unix. the program is executing only for the number generation in the main program. but when i call the predefined function . the output retrieved is incorrect. Can someone please correct me where i have done it wrong
My main program states as
#include <stdio.h>
#include <stdlib.h>
void sort1(int []);
int main(void) {
int array[100];
int i , j;
printf("ten random numbers in [1,1000]\n");
for (i = 1; i <= 10; i++)
{
generate random numbers
}
printf("The list of Hundred random numbers are \n");
for (j = 1; j <= 10; j++ )
{
//to print the random numbers
}
sort1(array);
return 0;
}
//this is my user defined function: sort1.c
include <stdio.h>
int sort1(int a[])
{
int array[100], i, d, swap, e=10;
// algortithm
}
}
printf("Sorted list in ascending order:\n");
for ( i= 0 ; i< e ; i++ )
printf("%d\n", array[i]);
}
I get the output as
ten random numbers in [1,1000]
The list of Hundred random numbers are
--This gives correct output
Sorted list in ascending order:
1
-1442229816
0
-1444472964
#include <stdio.h>
#include <stdlib.h>
void sort1(int []);
int main(void) {
int array[100];
int i;
printf("ten random numbers in [1,1000]\n");
for (i = 1; i <= 10; i++)
{
array[i] = rand()%1000 + 1;
}
printf("The list of Hundred random numbers are \n");
for (i = 1; i <= 10; i++ )
{
printf("Element[%d] = %d\n", i, array[i] );
}
//Up to here it's ok but you have set the values in the positions 1-10 of the array so keep consistent with it
sort1(array);
printf("Sorted list in ascending order:\n");
for ( i= 1 ; i<= 10 ; i++ )
printf("%d\n", array[i]);
return 0;
}
void sort1(int a[])
{
int i,swap,sorted=0;
while(sorted==0){ //flag to know if array is sorted 0 means not sorted.
sorted=1; // we set the flag to sorted at start of sweep
for (i= 1 ; i<= (10-1); i++) //sweep through array
{
if (a[i] > a[i+1]) //check if sorted
{
swap = a[i];
a[i] = a[i+1];
a[i+1] = swap;
sorted=0; //if not sorted set flag to 0 and swap
}
}
}
}
Main problems in your code:
1) array[100] is not initialized in sort1 function.
2) I do not understand your sorting algorithm but in any case you are checking for values of a[0] which are not initialized so take care of the array positions you use each time and be consistent with it.
3) function prototype does not match

Resources