Jump to content

mctrivia

Members
  • Posts

    29
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

mctrivia's Achievements

Member

Member (2/5)

0

Reputation

  1. as promised here is some working code. I am sure there are ways to optimize. especially when to end(I am using iteration count only). But it works <?php define("POINT_COUNT",6); function lockToSphere($points) { $temp=array(); foreach ($points as $point) { //get length $length=sqrt($point[0]*$point[0]+$point[1]*$point[1]+$point[2]*$point[2]); //normalize to 1 $temp[]=array($point[0]/$length,$point[1]/$length,$point[2]/$length); } return $temp; } //generate random points in a 1x1x1 cube $points=array(); for ($i=0;$i<POINT_COUNT;$i++) { $points[]=array(rand(1,1000)/1000,rand(1,1000)/1000,rand(1,1000)/1000); } //lock points to sphere $points=lockToSphere($points); $m=10; for ($i=0;$i<10000;$i++) { if ($i%10==0) { $m/=1.01; } foreach ($points as $ka=>$pa) { $forces=array(0,0,0); foreach ($points as $kb=>$pb) { if ($ka!=$kb) { //compute difference between 2 points $d=array($pb[0]-$pa[0],$pb[1]-$pa[1],$pb[2]-$pa[2]); //compute forces $distance=sqrt($d[0]*$d[0]+$d[1]*$d[1]+$d[2]*$d[2]); $multiplier=1/($distance*$distance*$distance); //force/distance = 1/distance^3 $forces[0]-=$d[0]*$multiplier; $forces[1]-=$d[1]*$multiplier; $forces[2]-=$d[2]*$multiplier; } } //move point by scaled amount $temp[$ka]=array($pa[0]+$m*$forces[0],$pa[1]+$m*$forces[1],$pa[2]+$m*$forces[2]); } $points=lockToSphere($temp); } //print points foreach ($points as $point) { echo $point[0] . ',' . $point[1] . ',' . $point[2] . '<br>'; } ?>
  2. I ditched this whole version and instead went 3d points. the pressure pushes the points off the sphere so after each iteration I normalize the distance to the center to 1. Works perfectly. will post code later when I find my thumb drive so I don't get typos from retyping.
  3. there is no parsing error. there is just no edit option to allow me to put the ; back in. My test system has no internet connection so I just retyped the code out. I missed 1 ;
  4. the ; got lost from the end of the one line when I copied over. Not the problem as it is present on my machine.
  5. There is a cool flash app here http://members.ozemail.com.au/~llan/mpol.html that computes a shape with as far apart as possible points(keeping them on the outer plane of a sphere) using the inverse square law and multiple iterations. Problem is the flash dies if you try to run really high point counts and I wanted to be able to mess with the physics by adding fixed points. All i need is the x,y,z cowardinents of each point so I can tell my 3d printer how to print the shape. I tried to convert the script to PHP. Problem is no matter what I try I get meaningless results out. My theory was if I map my points on a longitude and latitude grid of a circle of radius 1 I could simplify things a fair bit. My complete source code is at the bottom but let me break down what I believe each section should do. In forces function it computes the distance between 2 points using the formula: acos(sin($lat1)*sin($lat2) + cos($lat1)*cos($lat2) * cos($lon2-$lon1)); since I went with a gravitational constant of 1 a radius of 1 and a mass for each point of 1 the repulsion force between the points should be: 1/($distance*$distance) direction of this force clockwise from North should be $y=sin($lon2-$lon1) * cos($lat2); $x=cos($lat1)*sin($lat2)-sin($lat1)*cos($lat2)*cos($lon2-$lon1); //invert direction $direction=atan2($y,$x)+pi(); function addResults splits the force and bearing up into rectangular format so I can add all the vectors easier then function move takes the sum off all the vectors multiplies it by a scaler to limit the points movement and computes the new point. Problem is it does not work. I have definitely screwed up. Any help would greatly be appreciated. <?php define("POINT_COUNT",6); function forces($a,$b,&$force,&$direction) { //break up arrays $lat1=$a[0]; $lat2=$b[0]; $lon1=$a[1]; $lon2=$b[1]; //compute distance between points $distance=acos(sin($lat1)*sin($lat2) + cos($lat1)*cos($lat2) * cos($lon2-$lon1)); //compute force direction if ($distance==0) { $direction=rand(0,2*pi()); //pick random direction $distance=0.1; } else { $y=sin($lon2-$lon1) * cos($lat2); $x=cos($lat1)*sin($lat2)-sin($lat1)*cos($lat2)*cos($lon2-$lon1); //invert direction $direction=atan2($y,$x)+pi(); } $force=1/($distance*$distance) } function move($point,$forcex,$forcey,$m) { $lat1=$point[0]; $lon1=$point[1]; $d=sqrt(pow($forcex*$m,2)+pow($forcey*$m,2)); if ($forcex==0) { $forcex=0.000001; } $bearing=atan(((0-$forcey)*$m)/((0-$forcex)*$m)); $lat2=asin(sin($lat1)*cos($d)+cos($lat1)*sin($d)*cos($bearing)); $lon2=$lon1+atan2(sin($bearing)*sin($d)*cos($lat1),cos($d)-sin($lat1)*sin($lat2)); return array($lat2,$lon2); } function sphereTo3D($point) { $lat=$point[0]; $lon=$point[1]; //get radius of x/y circle $r=cos($lat); //get z position $z=sin($lat); //calculate xy if ($r==0) { $x=0; $y=0; } else { $x=cos($lon)*$r; $y=sin($lon)*$r; } return array($x,$y,$z); } function addResult(&$x,&$y,$force,$theta) { $x+=cos($theta)*$force; $y+=sin($theta)*$force; } //latitude and longitue $points=array(); //add the number of points needed for ($i=0; $i<POINT_COUNT; $i++) { $points[]=array(deg2rad(rand(0,36000)/100),deg2rad(rand(0,36000)/100)); } for($i=0;$i<1000;$i++) { $m=0.01; //compute forces between each point $temp=array(); foreach ($points as $key=>$point) { //compute force on point $forcex=0; $forcey=0; foreach ($points as $a=>$b) { if ($key!=$a) { forces($point,$b,$force,$direction); addResult($forcex,$forcey,$force,$direction); } } //determine what should do to try and nutralize force $temp[]=move($point,$forcex,$forcey,$m); } $points=$temp; } //convert to 3d foreach ($points as &$point) { $point=sphereTo3D($point); echo $point[0] . ',' . $point[1] . ',' . $point[2] .'<br>'; } ?>
  6. thanks I will initialize the garbage collector and see what else I can do to save memory.
  7. those are all temporary. i thought if they were within a function they were automatically given up when function completed.
  8. because i can only set cron jobs to run every minute. not every second.
  9. class Point { protected $v=array(); public function __construct() { $this->id=++$_GET['zzz_library_model_lastpoint']; if (func_num_args()==0) { $this->v=array(0,0,0); } elseif (func_num_args()==1) { $this->v=func_get_arg(0); } elseif (func_num_args()==3) { $this->v=array(func_get_arg(0),func_get_arg(1),func_get_arg(2)); } } public function __get($name) { switch (strtolower($name)) { case 'x': return $this->v[0]; break; case 'y': return $this->v[1]; break; case 'z': return $this->v[2]; break; case 'v': return $this->v; break; case 'id': return $this->id; break; } } public function __set($name,$value) { switch (strtolower($name)) { case 'x': $this->v[0]=$value; break; case 'y': $this->v[1]=$value; break; case 'z': $this->v[2]=$value; break; case 'v': $this->v=$value; break; } } public function __toString() { return pack("f",$this->v[0]) . pack("f",$this->v[1]) . pack("f",$this->v[2]); } public function rotate($theta,$axis) { //theta in rads use deg2rad() if you want deg. switch (strtolower($axis)) { case 'x': $x=cos($theta)*$this->v[1]-sin($theta)*$this->v[2]; $y=sin($theta)*$this->v[1]+cos($theta)*$this->v[2]; $this->v[1]=$x; $this->v[2]=$y; break; case 'y': $x=cos($theta)*$this->v[2]-sin($theta)*$this->v[0]; $y=sin($theta)*$this->v[2]+cos($theta)*$this->v[0]; $this->v[2]=$x; $this->v[0]=$y; break; case 'z': $x=cos($theta)*$this->v[0]-sin($theta)*$this->v[1]; $y=sin($theta)*$this->v[0]+cos($theta)*$this->v[1]; $this->v[0]=$x; $this->v[1]=$y; break; } } public function translate($x,$y,$z) { $this->v[0]+=$x; $this->v[1]+=$y; $this->v[2]+=$z; } public function scale($x,$y=null,$z=1) { if ($y==null) { $y=$x; $z=$x; } $this->v[0]*=$x; $this->v[1]*=$y; $this->v[2]*=$z; } } // ************************************************************************************************************ class Triangle { protected $v=array(); public function __construct() { if (func_num_args()==0) { $this->v=array(new Point(),new Point(),new Point()); } elseif (func_num_args()==3) { $this->v=array(func_get_arg(0),func_get_arg(1),func_get_arg(2)); } elseif (func_num_args()==9) { $this->v=array( new Point(func_get_arg(0),func_get_arg(1),func_get_arg(2)), new Point(func_get_arg(3),func_get_arg(4),func_get_arg(5)), new Point(func_get_arg(6),func_get_arg(7),func_get_arg() ); } return !(($v[0]==$v[1]) || ($v[0]==$v[2]) || ($v[1]==$v[2])); } public function __set($name,$value) { $t=substr($value,1,1); switch (substr(strtolower($name),0,1)) { case 'x': $this->v[$t]->x=$value; break; case 'y': $this->v[$t]->y=$value; break; case 'z': $this->v[$t]->z=$value; break; case 'v': $this->v[$t]=$value; break; } } public function __get($name) { if (strlen($name>1)) { $t=substr($value,1,1); } else { $t=0; } switch (substr(strtolower($name),0,1)) { case 'x': return $this->v[$t]->x; break; case 'y': return $this->v[$t]->y; break; case 'z': return $this->v[$t]->z; break; case 'v': return $this->v[$t]; break; case 'p': return array_merge($this->v[0]->v,$this->v[1]->v,$this->v[2]->v); break; case 'n': return $this->getNormal(); break; } } public function __toString() { $n=$this->getNormal(); return pack("f",$n[0]).pack("f",$n[1]).pack("f",$n[2]).$this->v[0].$this->v[1].$this->v[2].pack("S",0); } protected function getNormal() { $a0=$this->v[2]->x-$this->v[0]->x; $a1=$this->v[2]->y-$this->v[0]->y; $a2=$this->v[2]->z-$this->v[0]->z; $b0=$this->v[1]->x-$this->v[0]->x; $b1=$this->v[1]->y-$this->v[0]->y; $b2=$this->v[1]->z-$this->v[0]->z; $x=$a1*$b2-$a2*$b1; $y=$a2*$b0-$a0*$b2; $z=$a0*$b1-$a1*$b0; $l=sqrt($x*$x+$y*$y+$z*$z); $x/=$l; $y/=$l; $z/=$l; return array($x,$y,$z); } }
  10. is it possible to get an ajax call to execute a php script that will continue running even if the user leaves my site? i have used cron jobs and tokens to do in past but this can result in 1 min delay.
  11. i guess my question is what makes an object require ram? i would think point would need only enough space to store the 3 floats and triangle need only enough space to hold the 3 pointers. my math says 80 bit*3 for floats+64bit*3 for pointers=54 bytes but 1000 are being used.
  12. i have made 2 classes. the first "point" has an array of 3 floats and some functions to help manipulate the values the second "triangle" has an array of 3 points. in this way if i want to make a tetrahedron i can make 4 points and assign them to 4 triangles. problem is for some reason each triangle point group seems to use 1k. any idea why?
  13. i am trying to find a way to render stl files into a jpg from a specific angle. any ideas? i have no clue.
  14. I found a bug in the code where 1==1 would fail the test but shouldn't. Fixed now. Please let e know if you have tried to hack but failed. I think I have made this bulit proof but there are alot smarter people on here then me. <?php $whitefunc=array( 'abs()','acos()','acosh()','asin()','asinh()','atan()','atan2()','atanh()', 'base_convert()','bindec()', 'ceil()','cos()','cosh()', 'decbin()','dechex()','decoct()','deg2rad()', 'exp()','expm1()', 'floor()','fmod()', 'getrandmax()', 'hexdec()','hypot()', 'is_finite()','is_infinite()','is_nan()', 'lcg_value()','log()','log10()','log1p()', 'max()','min()','mt_getrandmax()','mt_rand()','mt_srand()', 'octdec()', 'pi()','pow()', 'rad2deg()','rand()','round()', 'sin()','sinh()','sqrt()','srand()', 'tan()','tanh()' ); function breakBracket($code,$a="(",$b=")") { $parts=array(); $end=strpos($code,$b); while (!($end===false)) { //find first start bracket before point $start=strrpos(substr($code,0,$end),$a); //find first end bracket after point $end=strpos($code,$b,$start); //break inner brackets out $parts[]=substr($code,$start+1,$end-$start-1); $code=substr($code,0,$start) . '|~-~|' . substr($code,$end+1); //find next search point $end=strrpos($code,$b); } $parts[]=$code; //replace '|~-~|' with brackets foreach ($parts as &$part) { $part=preg_replace("/\|~-~\|/", "()", $part); } return $parts; } function check_syntax($code) { return @eval('return true;' . $code); } function testRule($rule) { //replace $1,$2,$3... in rules with tag code parts $rule=preg_replace_callback( '/\$[0-9]+/', create_function( // single quotes are essential here, // or alternative escape all $ as \$ '$matches', 'return "53";' ), $rule ); //check for ilegal characters ;'" $good=(!(preg_match("/([;'\"])/",$rule)>0)); //check for assingments if ($good) { $temp=preg_replace('/([=!]==)/','',$rule); $temp=preg_replace('/([=<>!]=)/','',$temp); $good=(!(preg_match("/(=)/",temp)>0)); } //see if rule is valid if ($good) { $good=check_syntax('(' . $rule . ');'); } //check if rule only contains good functions, numbers, brackets, and math operators if ($good) { //break inside of () out of outsides $ruleparts=breakBracket($rule); //combine all parts together into 1 string $str=''; foreach ($ruleparts as $part) { $str.=$part . ' '; } //remove all valid functions global $whitefunc; //array of lower case functions user may use with () after name $str=str_replace($whitefunc,' ',$str); //remove all valid characters $str=preg_replace('|([0-9\(\)\+\-\*\/\%=])|',' ',$str); //if anything left call error if (preg_match_all("/([^ ])/",$str,$out)) { $good=false; } } return $good; } $rules=array( 'acos(56)+(ceil(56)-3+5)', 'sin(45)-35', 'a+$1*4', '1==1', 'hacked=true', ); echo '<table border="1">'; foreach($rules as $rule) { echo '<tr><td>' . $rule . '</td><td>'; if (testRule($rule)) { echo ' </td><td>Pass'; } else { echo ' </td><td>Fail'; } echo '</td></tr>'; } echo '</table>'; ?>
  15. The following is my test preprocessing script so far. Any code before it can be run through the dangerous if (eval('return (' . $rule . ');')) { will pass. at the bottom $rules is an array of rules to test. It will print out the rule and if they pass or fail the test. Can anyone find a way to get a bad rule to pass this test or use this script itself to hack my server. If you have an idea how to improve is greatly appreciated also. <?php $whitefunc=array( 'abs()','acos()','acosh()','asin()','asinh()','atan()','atan2()','atanh()', 'base_convert()','bindec()', 'ceil()','cos()','cosh()', 'decbin()','dechex()','decoct()','deg2rad()', 'exp()','expm1()', 'floor()','fmod()', 'getrandmax()', 'hexdec()','hypot()', 'is_finite()','is_infinite()','is_nan()', 'lcg_value()','log()','log10()','log1p()', 'max()','min()','mt_getrandmax()','mt_rand()','mt_srand()', 'octdec()', 'pi()','pow()', 'rad2deg()','rand()','round()', 'sin()','sinh()','sqrt()','srand()', 'tan()','tanh()' ); function breakBracket($code,$a="(",$b=")") { $parts=array(); $end=strpos($code,$b); while (!($end===false)) { //find first start bracket before point $start=strrpos(substr($code,0,$end),$a); //find first end bracket after point $end=strpos($code,$b,$start); //break inner brackets out $parts[]=substr($code,$start+1,$end-$start-1); $code=substr($code,0,$start) . '|~-~|' . substr($code,$end+1); //find next search point $end=strrpos($code,$b); } $parts[]=$code; //replace '|~-~|' with brackets foreach ($parts as &$part) { $part=preg_replace("/\|~-~\|/", "()", $part); } return $parts; } function check_syntax($code) { return @eval('return true;' . $code); } function testRule($rule) { //replace $1,$2,$3... in rules with tag code parts $rule=preg_replace_callback( '/\$[0-9]+/', create_function( // single quotes are essential here, // or alternative escape all $ as \$ '$matches', 'return "53";' ), $rule ); //check for ilegal characters ;'" $good=(!(preg_match("/([;'\"])/",$rule)>0)); //check for assingments if ($good) { $temp=preg_replace('/([=!]==)/','',$rule); $temp=preg_replace('/([=<>!]=)/','',$temp); $good=(!(preg_match("/(=)/",temp)>0)); } //see if rule is valid if ($good) { $good=check_syntax('(' . $rule . ');'); } //check if rule only contains good functions, numbers, brackets, and math operators if ($good) { //break inside of () out of outsides $ruleparts=breakBracket($rule); //combine all parts together into 1 string $str=''; foreach ($ruleparts as $part) { $str.=$part . ' '; } //remove all valid functions global $whitefunc; //array of lower case functions user may use with () after name $str=str_replace($whitefunc,' ',$str); //remove all valid characters $str=preg_replace('|([0-9\(\)\+\-\*\/\%])|',' ',$str); //if anything left call error if (preg_match_all("/([^ ])/",$str,$out)) { $good=false; } } return $good; } $rules=array( 'acos(56)+(ceil(56)-3+5)', 'sin(45)-35', 'a+$1*4', ); echo '<table border="1">'; foreach($rules as $rule) { echo '<tr><td>' . $rule . '</td><td>'; if (testRule($rule)) { echo ' </td><td>Pass'; } else { echo ' </td><td>Fail'; } echo '</td></tr>'; } echo '</table>'; ?>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.