Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-23-2006, 01:34 AM   PM User | #1
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
Geometry.

I was playing around and made this... but it can be usefull... if you'll ever need to do such things.

Ah yes descritption.
It'll draw pretty much anything as long as you give coordinates.
It'll give you the length of the line and return the point at it's middle.
For polygons it'll return the area (with the exception that the polygion intersects with itself) and perimeter.
An example is given below.
PHP Code:
<?php
class point
{
    public 
$x 0,
            
$y 0;

    function 
__construct($x 0$y 0)
    {
        
$this->set($x$y);
    }

    function 
set($x$y)
    {
        if(!
is_nan($x) && !is_nan($y))
        {
            
$this->$x;
            
$this->$y;
            return 
true;
        }
        return 
false;
    }

    function 
pos()
    {
        return array(
$this->x$this->y);
    }

    function 
move_by($vector)
    {
        if(!
$vector instanceof vector)
        {
            return 
false;
        }
        return 
$this->set($this->$vector->x$this->$vector->y);
    }
}

class 
line
{
    public 
$points = array();
    public 
$length 0;

    function 
__construct($a false$b false)
    {
        return 
$this->set($a$b);
    }

    function 
set($a$b)
    {
        if(
$a instanceof point && $b instanceof point)
        {
            
$this->points[] = $a;
            
$this->points[] = $b;
            
$this->length();
            return 
true;
        }
        return 
false;
    }

    private function 
length()
    {
        
$this->length sqrt((pow($this->points[1]->$this->points[0]->x2)) + (pow($this->points[1]->$this->points[0]->y2)));
    }

    function 
middle()
    {
        return new 
point(($this->points[0]->$this->points[1]->x) / 2, ($this->points[0]->$this->points[1]->y) / 2);
    }
}

class 
vector
{
    public 
$x,
            
$y;

    function 
__construct($x$y)
    {
        
$this->set($x$y);
    }

    function 
set($x$y)
    {
        
$this->= (int) $x;
        
$this->= (int) $y;
    }
}

class 
polygon
{
    public 
$points = array();
    public 
$area 0;
    public 
$perimeter 0;

    function 
__construct()
    {
        
$n func_num_args();
        if(
$n 3)
        {
            return 
false;
        }
        for(
$i 0$i $n$i++)
        {
            if(
func_get_arg($i) instanceof point)
            {
                
$this->points[] = func_get_arg($i);
            }
        }
        
$this->area();
        
$this->perimeter();
    }

    private function 
area()
    {
        
$area 0;
        for(
$i 0$n count($this->points) - 1$i $n$i++)
        {
            
$area += $this->points[$i]->$this->points[$i 1]->$this->points[$i 1]->$this->points[$i]->y;
        }
        
$area += $this->points[$n]->$this->points[0]->$this->points[0]->$this->points[$n]->y;
        
$area /= 2;
        
$area abs($area);
        
$this->area $area;
    }

    private function 
perimeter()
    {
        
$perimeter 0;
        for(
$i 0$n count($this->points) - 1$i $n$i++)
        {
            
$side = new line($this->points[$i], $this->points[$i 1]);
            
$perimeter += $side->length;
        
//    var_dump($perimeter);
        
}
        
$side = new line($this->points[$n], $this->points[0]);
        
$perimeter += $side->length;
        
$this->perimeter $perimeter;
    }

    function 
move_by($vector)
    {
        if(!
$vector instanceof vector)
        {
            return 
false;
        }
        for(
$i 0$n count($this->points); $i $n$i++)
        {
            
$this->points[$i]->set($this->points[$i]->$vector->x$this->points[$i]->$vector->y);
        }
        return 
true;
    }
}

class 
grid
{
    private 
$size = array(),
            
$data = array(),
            
$im null;


    function 
__construct($min_x$max_x$min_y$max_y$scale_x$scale_y)
    {
        
$this->size['min'] = array($min_x$max_y);
        
$this->size['max'] = array($max_x$min_y);
        
$this->size['scale'] = array($scale_x$scale_y);
        
$this->size[0] = array($scale_x $min_x$scale_y $max_y);

        return 
$this->create_image();
    }

    private function 
create_image()
    {
        
$this->im imagecreate(+ ($this->size['min'][0] + $this->size['max'][0]) * $this->size['scale'][0], + ($this->size['min'][1] + $this->size['max'][1]) * $this->size['scale'][1]);
        
$back imagecolorallocatealpha($this->im255255255127); // transparent.
        
return true;
    }

    function 
plot($obj$color false)
    {
        if(!
$color)
        {
            
$color imagecolorallocate($this->im000);
        }
        switch(
get_class($obj))
        {
            case 
'point':
                
imagefilledellipse($this->im$this->size[0][0] + ($obj->$this->size['scale'][0]), $this->size[0][1] - ($obj->$this->size['scale'][1]), 33$color);
            break;
            case 
'line':
                
imageline($this->im$this->size[0][0] + ($obj->points[0]->$this->size['scale'][0]), $this->size[0][1] - ($obj->points[0]->$this->size['scale'][1]), $this->size[0][0] + ($obj->points[1]->$this->size['scale'][0]), $this->size[0][1] - ($obj->points[1]->$this->size['scale'][1]), $color);
            break;
            case 
'polygon':
                
$data = array();
                for(
$i 0$n count($obj->points); $i $n$i++)
                {
                    
$data[] = $this->size[0][0] + ($obj->points[$i]->$this->size['scale'][0]);
                    
$data[] = $this->size[0][1] - ($obj->points[$i]->$this->size['scale'][1]);
                }
                
imagefilledpolygon($this->im$data$n$color);
            break;
        }
    }

    function 
plot_axis()
    {
        
$black imagecolorallocate($this->im000);
        
imageline($this->im$this->size['min'][0] * $this->size['scale'][0], 0$this->size['min'][0] * $this->size['scale'][0], ($this->size['min'][1] + $this->size['max'][1]) * $this->size['scale'][1], $black);
        
imageline($this->im0$this->size['min'][1] * $this->size['scale'][1], ($this->size['min'][0] + $this->size['max'][0]) * $this->size['scale'][0], $this->size['min'][1] * $this->size['scale'][1], $black);
    }

    function 
func($eval '$x'$percision 10$color false)
    {
        if(!
$color)
        {
            
$color imagecolorallocate($this->im000);
        }
        if(
substr($eval, -1) != ';')
        {
            
$eval .= ';';
        }
        for(
$x = -* ($this->size['min'][0] + $this->size['max'][0]), $n = ($this->size['min'][0] + $this->size['max'][0]) * $percision$x $n$x += $percision)
        {
            
$points[] = array($this->size[0][0] + ($x $this->size['scale'][0]), $this->size[0][1] - (eval('return ' $eval) * $this->size['scale'][1]));
        }
        for(
$i 0$n count($points) - 1$i $n$i++)
        {
            
imageline($this->im$points[$i][0], $points[$i][1], $points[$i 1][0], $points[$i 1][1], $color);
        }
    }

    function 
make_image($dest false)
    {
        if(!
$dest)
        {
            
header('Content-Type: image/png');
            return 
imagepng($this->im);
        }
        return 
imagepng($this->im$dest);
    }

    function 
color($r$g$b$alpha)
    {
        return 
imagecolorallocatealpha($this->im$r$g$b$alpha);
    }
}
?>
Example:
PHP Code:
<?php
$poly 
= new polygon(
    new 
point(00),
    new 
point(01),
    new 
point(12),
    new 
point(22),
    new 
point(23),
    new 
point(34),
    new 
point(44),
    new 
point(53),
    new 
point(50),
    new 
point(40),
    new 
point(43),
    new 
point(33),
    new 
point(32),
    new 
point(21),
    new 
point(11)
);
$point = new point(44);
$line = new line(new point(-22), new point(45));
$poly2 = new polygon(
    new 
point(00),
    new 
point(04),
    
$point,
    new 
point(40),
    new 
point(30),
    new 
point(33),
    new 
point(13),
    new 
point(10)
);

$vector = new vector(-53);


$grid = new grid(101010102020);
$grid->plot_axis();
$grid->plot($point);
$grid->plot($poly$grid->color(1002001000));

$poly->move_by($vector);

$grid->func('pow($x, 2) + $x'10);
$grid->make_image(/*'C:/test.png'*/);
It can produce an image like the one attached (PNG not GIF but png isn't allowed here for some reason).
Attached Thumbnails
Click image for larger version

Name:	test.gif
Views:	467
Size:	2.2 KB
ID:	4194  
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.

Last edited by marek_mar; 01-23-2006 at 10:46 PM..
marek_mar is offline   Reply With Quote
Old 01-23-2006, 10:33 PM   PM User | #2
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
Be usefull for a extremely random captcha image. Or, if you really into it you could spend the time to make image generators, as in,if you have ever used that doll maker that makes dolls that look similar to the 'homies' in the quarter machines. It uses PHP to draw the characters (all designs are unditable) allowing you to customize colors and add a shirt logo. Pretty neat stuff.
Element is offline   Reply With Quote
Old 01-23-2006, 10:47 PM   PM User | #3
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
Well I didn't think about any of these while writing that.
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.
marek_mar is offline   Reply With Quote
Old 01-23-2006, 10:55 PM   PM User | #4
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
Well then it lets you know its usefull.
Element is offline   Reply With Quote
Old 01-24-2006, 11:49 PM   PM User | #5
Jalenack
Regular Coder

 
Join Date: May 2004
Location: Berkeley, California Age: 15
Posts: 398
Thanks: 0
Thanked 0 Times in 0 Posts
Jalenack is an unknown quantity at this point
Cool!

I spent 15 mins or so making it slightly more useful: type in a function and it graphs instantly...

graph.php

PHP Code:
<?php
include('class.php');

$grid = new grid(101010102020);
$grid->plot_axis();

$y split("="$_SERVER['QUERY_STRING']);
$x rawurldecode($y[1]);

$grid->func($x10);
$md5 md5(microtime(true));
$grid->make_image($md5.".png");

header("location: {$md5}.png");
?>
class.php
...See first box of code in marek's first post -- basically contains all the math

index.php

Code:
<html>
<head>
<title>Function Grapher</title>

<script type="text/javascript">
function img_switch (that) {
	im = document.getElementsByTagName('img')[0];
	im.src = "graph.php?func="+escape(that.value);
}

</script>

</head>

<body>
<h1>Function Grapher</h1>

<img src="graph.php?func=$x+2" height="401" width="401" />

<input type="text" value="$x+2" onkeyup="img_switch(this)" size="100" />

</body>

</html>
I'm not going to give you a preview because of the security risks...eval($_GET) eek!
__________________
Jalenack.com .:. YWDA Founder .:. Rounded Corners Maker 1.1! .:. My Blog
The hardest thing about teaching is not knowing the right answers, but knowing the right questions - Elisabeth Klein
Pretty buttons does not a great website make.
Jalenack is offline   Reply With Quote
Old 01-25-2006, 03:27 AM   PM User | #6
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
Evaling input is not secure (I know that - don't copy that). If you want to secure it you could use this class that evaluates math securely.
grid::func() was the last method I've added (all I wanted was to draw polygons) so if you want to use it in public it would be wise to secure it.
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.
marek_mar is offline   Reply With Quote
Old 04-26-2006, 10:00 AM   PM User | #7
sir pannels
Regular Coder

 
Join Date: Jun 2002
Posts: 905
Thanks: 23
Thanked 5 Times in 5 Posts
sir pannels is an unknown quantity at this point
hi,

i get an error with this one, no image displays.. error..
Quote:
[26-Apr-2006 09:55:27] PHP Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in c:\phpdev5\www\public\new folder\class.php on line 4
[26-Apr-2006 09:55:27] PHP Fatal error: Cannot instantiate non-existent class: grid in c:\phpdev5\www\public\new folder\graph.php on line 4
tried this on two servers running different php versions .. still no luck,
any thoughts? Im using the second code and M's class.php

cheers
p
sir pannels is offline   Reply With Quote
Old 04-26-2006, 08:30 PM   PM User | #8
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
You need at least PHP5 to run the code.
PHP4 does not recognise the public/private/protected properties (it uses var only) and that's why you get the parse error.
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.
marek_mar is offline   Reply With Quote
Old 04-27-2006, 12:52 PM   PM User | #9
sir pannels
Regular Coder

 
Join Date: Jun 2002
Posts: 905
Thanks: 23
Thanked 5 Times in 5 Posts
sir pannels is an unknown quantity at this point
ah ok. thanks =]
sir pannels is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:36 AM.


Advertisement
Log in to turn off these ads.