PDA

View Full Version : Programming Question


thirddan
04-15-2005, 01:04 AM
anybody out there know how to write the equation for a vector that will represent a spherical volume of radius r with center point at (x, y, z) ???


thanks...

The Goober
04-15-2005, 01:08 AM
huh?

TimM
04-15-2005, 01:10 AM
[ QUOTE ]
huh?

[/ QUOTE ]

fluxrad
04-15-2005, 01:10 AM
[ QUOTE ]
anybody out there know how to write the equation for a vector that will represent a spherical volume of radius r with center point at (x, y, z) ???

[/ QUOTE ]

f(x)=xE(3y/z^x)sub3(sin(z^y)

If that doesn't work, let me know.

mason55
04-15-2005, 01:10 AM
(4/3) (pi) r^3 like i said i'm drunk please ignore that.

i'm drunk but i think that's what you want?

thirddan
04-15-2005, 01:17 AM
sorry, im kinda a novice script writer...this is for a 3d application that will basically fill a sphere volume with particles based on a fractal...in order for the fractal to keep within the sphere i have to limit it...

i have to do this with different shapes in 3d space...

here is the box equation -
vector $p = 1 * (rand(<<($cubeX - ($cubeWidth / 2)),($cubeY - ($cubeHeight / 2)), ($cubeZ - ($cubeWidth / 2))>>, <<($cubeX + ($cubeWidth / 2)),($cubeY + ($cubeHeight / 2)), ($cubeZ + ($cubeWidth / 2))>>));

basically the $cubeX/Y/Z are user defined to represent the center of the box while height is height and width is width...

could you explain your equation? thanks

fluxrad
04-15-2005, 01:23 AM
i have no idea how to do what you're asking. the equation was completely fake.

The Goober
04-15-2005, 01:26 AM
what language is that? What exactly are your inputs and outputs here? Are you looking for a function that will generate points randomly distributed inside a sphere?

thirddan
04-15-2005, 01:32 AM
the language is MEL (maya embedded language) its for a 3d application called Maya, its similar to C...

i have a fractal function that will sample different points within space and if the fractal returns a value within a certain threshhold it will create a particle (a dot) at that point, if it is outside the thres it will move to another point without creating anything...

vector $p = 1 * (rand(<<($cubeX - ($cubeWidth / 2)),($cubeY - ($cubeHeight / 2)), ($cubeZ - ($cubeWidth / 2))>>, <<($cubeX + ($cubeWidth / 2)),($cubeY + ($cubeHeight / 2)), ($cubeZ + ($cubeWidth / 2))>>));

this takes a random point along a vector...this vector is for a box shape, the first point along the vector is denoted as <<x, y, z>> and the second is <<x1, y1, z1>>

meh...does this make any sense or too hard to do over the internet?

TimM
04-15-2005, 01:33 AM
Ok now I think I understand what you are asking. I don't know the application specific stuff, but here is the math stuff.

If the center is x0,y0,z0 and the radius r, for any point x,y,z to be inside the sphere, the following equation must be true:

sqrt((x - x0)^2 + (y - y0)^2 + (z - z0)^2) < r

The Goober
04-15-2005, 01:49 AM
Ah, I think I understand now. Its fuuny, because I'm actually due to start learning MEL for work soon.

If that rand() function with those arguments is giving you points randomly distributed within a box, then that is all its going to give you. The two points that you are specifying are actually the corners of the (axis-aligned) box - there is no way to use that function to generate points within a sphere. That is, I think what its computing is: p = rand(<x,y,z>,<x1,y,z1>) returns a point p such that x<=p.x<=x1, y<=p.y<=y1, z<=p.z<=z1. I'm guessing here, but seems to make sense.

So am I correct that what you are looking for is a function that will randomly generate points within a sphere?

thirddan
04-15-2005, 02:00 AM
yeah, it will pick random points within the confines of the vector that i give it, then run the point coordinates thru a fractal function and give them a threshold to either draw a particle or not...

my teacher gave us a hint that we would need to use the x^2 + y^2 + z^2 = r^2 forumla....

so far i have this ($sphereX/Y/Z/R are user defined)

float $sqrX = pow((0 - $sphereX), 2);
float $sqrY = pow((0 - $sphereY), 2);
float $sqrZ = pow((0 - $sphereZ), 2);
float $sqrR = pow($sphereR, 2);
float $result = sqrt($sqrX + $sqrY + $sqrZ);
if ($result < $sphereR)
{
vector $p = << ????? >>;
}
else
{
vector $p = << ?????? >>;
}

haha, you guys are the best, thanks for helping...

thirddan
04-15-2005, 02:12 AM
blah this is totally wrong...

TimM
04-15-2005, 02:24 AM
I think you need to use the random function on a cube that "circumscribes" the sphere, and some kind of loop to reject any points from that cube that are not in the sphere.

I don't even know the language so here is some pseudo code.

$result = $sphereR; // just so the loop executes at least once, and to clear the last value of $result
while ($result >= $sphereR)
{
$p = 1 * (rand(<<($sphereX - $sphereR),($sphereY - $sphereR), ($sphereZ - $sphereR)>>, <<($sphereX + $sphereR),($sphereY + $sphereR), ($sphereZ + $sphereR)>>)); // why the 1 * stuff?
$sqrX = pow((the x value of vector $p - $sphereX), 2);
$sqrY = pow((the y value of vector $p - $sphereY), 2);
$sqrZ = pow((the z value of vector $p - $sphereZ), 2);
$result = sqrt($sqrX + $sqrY + $sqrZ);
}

The Goober
04-15-2005, 02:38 AM
heh... i didn't realize I was doing your homework for you.

I still don't understand exactly what the question is. What are you given, and what must you compute?

If the problem is: given $sphereX/Y/Z/R and point $p, accept or reject $p, then its like this:

float $distSq = pow($p.x - $sphereX, 2) + pow($p.y - $sphereY), 2) + pow($p.z - $sphereZ, 2);

if($distSq <= ($sphereR * $sphereR))
{
// reject it somehow
}
else
{
// accept it somehow
}

(note that I'm using $p.x to mean the x component of $p, and I'm assuming that comments are the same C... I dunno the mel syntax, although I'd like to learn it)

thirddan
04-15-2005, 02:42 AM
haha, you arent doing my hw for me, my teacher gave us a fractal function to use, so im using it for something somewhat interesting and pretty useful...the eventual goal is to be able to take any arbitrary 3d object and fill it with particles using the fractal pattern...

TimM is on the right track, his code works pretty well, but i have to add to the loop so that it will only accept a certain defined amount of points...and once it finds taht many acceptable points it will exit the loop...

i really appreciate you guys helping me nerd out...

TimM
04-15-2005, 02:45 AM
Yeah my loop exits after finding one valid point, so you need a bigger loop around all of this to generate however many you need.

thirddan
04-15-2005, 03:15 AM
int $i;

for ($i = 0 ; $i < $numPart;)
{
vector $p = 1 * (rand(<<$sphereX - $sphereR, $sphereY - $sphereR, $sphereZ - $sphereR>>, <<$sphereX + $sphereR, $sphereY + $sphereR, $sphereZ + $sphereR>>));
float $sqrX = pow(($p.x - $sphereX), 2);
float $sqrY = pow(($p.y - $sphereY), 2);
float $sqrZ = pow(($p.z - $sphereZ), 2);
float $sqrR = pow($sphereR, 2);
float $result = sqrt($sqrX + $sqrY + $sqrZ);

if ($result < $sphereR)
{
float $fbm = fBm( $p, 0.8, 2, 3 );
$thresh = rand (0.4, 0.7);
if($fbm > $thresh)
{
emit -object $s[0] -position ($p.x) ($p.y) ($p.z);
$max--;
$i = $i + 1 ;
}
}
}


thanks guys...now i must sleep...