Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9

Thread: Geometry.

  1. #1
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts

    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 Attached Thumbnails Geometry.-test.gif  
    Last edited by marek_mar; 01-23-2006 at 10:46 PM.
    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.

  • #2
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    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.

  • #3
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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.

  • #4
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Well then it lets you know its usefull.

  • #5
    Regular Coder
    Join Date
    May 2004
    Location
    Berkeley, California Age: 15
    Posts
    398
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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.

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    hi,

    i get an error with this one, no image displays.. error..
    [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

  • #8
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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.

  • #9
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    ah ok. thanks =]


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •