Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

PHP Exercises: Compute the area of the polygon

PHP: Exercise-73 with Solution

A convex polygon is a simple polygon in which no line segment between two points on the boundary ever goes outside the polygon. Equivalently, it is a simple polygon whose interior is a convex set. In a convex polygon, all interior angles are less than or equal to 180 degrees, while in a strictly convex polygon all interior angles are strictly less than 180 degrees.
Write a PHP program that compute the area of the polygon . The vertices have the names vertex 1, vertex 2, vertex 3, ... vertex n according to the order of edge connections. However, n is 3 or more and 20 or less. You can also use the following formula to calculate the area S from the lengths a, b, and c of the triangle's three sides.

Input: Multiple character strings are given. One string is given per line

PHP: Compute the area of the polygon.

Pictorial Presentation:

PHP: Compute the area of the polygon.

Input:
Input is given in the following format.
x1 , y1
x2 , y2
:
xn , yn
xi , yi are real numbers representing the x and y coordinates of vertex i , respectively.

Sample Solution: -

PHP Code:

<?php
calc();
 
function calc() {
    $points = array();
    while ($line = trim(fgets(STDIN))) {
        list($x, $y) = explode(',', $line);
        $points[] = new Point($x, $y);
    }
    $sum = 0;
    $o = $points[0];
    for ($i = 1; $i < count($points) - 1; $i++) {
        $p1 = $points[$i];
        $p2 = $points[$i+1];
 
        $T = new Triangle($o, $p1, $p2);
        $sum += $T->getArea();
    }
    echo "Area of the polygon:\n";
    echo $sum . "\n";
}
 
class Triangle {
    public $A, $B, $C;
    public $a, $b, $c;
 
    public function __construct(Point $A, Point $B, Point $C) {
        $this->A = $A;
        $this->B = $B;
        $this->C = $C;
 
        $this->a = $B->distanceFrom($C);
        $this->b = $C->distanceFrom($A);
        $this->c = $A->distanceFrom($B);
    }
 
    public function getArea() {
        $z = ($this->a + $this->b + $this->c) / 2;
        return sqrt($z * ($z - $this->a) * ($z - $this->b) * ($z - $this->c));
    }
}
 
class Point {
    public $x, $y;
 
    public function __construct($x, $y) {
        $this->x = $x;
        $this->y = $y;
    }
 
    public function distanceFrom(Point $p) {
        $dx = $this->x - $p->x;
        $dy = $this->y - $p->y;
        return sqrt($dx * $dx + $dy * $dy);
    }
}
?>

Sample Input:
1.0, 0.0
0.0, 0.0
1.0, 1.0
2.0, 0.0
-1.0, 1.0

Sample Output:

Area of the polygon:
1.5

Flowchart:

Flowchart: Compute the area of the polygon.

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a PHP program to restore the original string by entering the compressed string with this rule.
Next: Write a PHP program to cut out words of 3 to 6 characters length from a given sentence not more than 1024 characters.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



PHP: Tips of the Day

How to Sort Multi-dimensional Array by Value?

Try a usort, If you are still on PHP 5.2 or earlier, you'll have to define a sorting function first:

Example:

function sortByOrder($a, $b) {
    return $a['order'] - $b['order'];
}

usort($myArray, 'sortByOrder');

Starting in PHP 5.3, you can use an anonymous function:

usort($myArray, function($a, $b) {
    return $a['order'] - $b['order'];
});

And finally with PHP 7 you can use the spaceship operator:

usort($myArray, function($a, $b) {
    return $a['order'] <=> $b['order'];
});

To extend this to multi-dimensional sorting, reference the second/third sorting elements if the first is zero - best explained below. You can also use this for sorting on sub-elements.

usort($myArray, function($a, $b) {
    $retval = $a['order'] <=> $b['order'];
    if ($retval == 0) {
        $retval = $a['suborder'] <=> $b['suborder'];
        if ($retval == 0) {
            $retval = $a['details']['subsuborder'] <=> $b['details']['subsuborder'];
        }
    }
    return $retval;
});

If you need to retain key associations, use uasort() - see comparison of array sorting functions in the manual

Ref : https://bit.ly/3i77vCC