reptile7's JavaScript blog
Monday, May 30, 2016
Meet Me at the Quad, Part 2
Blog Entry #368

On the borders

The next CCC offering is "Perimeter of a Quadrilateral", which was authored by Sam S. Lachterman in late 1997. The Perimeter of a Quadrilateral script asks the user for the width and length of a quadrilateral and then plugs those values into a var perimeter = (width * 2) + (length * 2); statement to give a perimeter, whose scope we will flesh out below.

The Perimeter of a Quadrilateral script code is available here and (excepting its copyright comment) is reproduced below:

<script language="javascript">

function perim( ) { var width = window.prompt("Enter the width of the quadrilateral:", ""); var length = window.prompt("Enter the length of the quadrilateral:", ""); var perimeter = (width * 2) + (length * 2); window.alert("The perimeter of the quadrilateral is " + perimeter + ". This was solved using the formula 2(l+w)."); }
<body bgcolor="#ffffff" onload="perim( );">

Quick comment on the alert( ) message: A 2(l+w) perimeter formula would require us to numberify the width and length, which would otherwise be concatenated and not added.

var perimeter = 2 * (Number(length) + Number(width));

Who's in and who's out

As quadrilateral types go, the (width * 2) + (length * 2) calculation duly applies to rectangles, and I guess we could say it applies to squares too, even as we would be stretching it to say that a square has a width and a length, but that's pretty much it.

A rectangle and a square

(I recognize that length and width are standard terms for the dimensions of a rectangle, but they really don't sit well with me: working with CSS conditions me to prefer width for the horizontal distance and height for the vertical distance.)

A more general (s1 * 2) + (s2 * 2) formulation could accommodate rhombuses, for which s1 and s2 are equal, and other parallelograms lacking right angles

A rhombus and a parallelogram without right angles

and also kites for which s1s2.

A kite

The (s1 * 2) + (s2 * 2) formula won't accommodate trapezoids and trapeziums, however.

A trapezoid and a trapezium

Parallelograms, kites, and trapezoids are convex quadrilaterals, for which both diagonals lie inside the perimeter.
A quadrilateral can be concave: one diagonal is inside the perimeter and one diagonal is outside the perimeter.
A quadrilateral can be complex ("self-intersecting"): both diagonals are outside the perimeter.
(As you would intuit, a trapezium can be convex, concave, or complex.)

A concave quadrilateral and a complex quadrilateral

(s1 * 2) + (s2 * 2) works for some concave and complex quadrilaterals but not for others.

The most general formula for calculating the perimeter of a quadrilateral, perimeter = s1 + s2 + s3 + s4, simply adds up the lengths of all four sides, and is applicable to all quadrilaterals, without exception. So get out your rulers and get measuring, folks.

You're so territorial

Sam also contributes a corresponding "Area of a Quadrilateral" script that asks the user for the width and length of a quadrilateral and then plugs those values into a var times = (width * length); statement to give a(n) times area. À la the Perimeter of a Quadrilateral script, the Area of a Quadrilateral script uses a prompt( ) interface to obtain the quadrilateral width and length and displays its output on an alert( ) box. The Area of a Quadrilateral script code is available here.

lw yea and nay

And how much mileage do we get with the width * length formula? Area = base × height is good for all parallelograms - close enough for government work, eh?

A parallelogram and its base and height

The gas runs out: width * length won't work for a kite that isn't a rhombus, for a trapezoid, or for any other kind of quadrilateral. As for a rhombus, we can get the area of a kite by multiplying the lengths of its diagonals and dividing the product by 2 (d1 * d2 / 2).

A kite and its two diagonals

We can get the area of a trapezoid by multiplying the average of its base lengths by its height.

There are a number of formulas for finding the area of a convex quadrilateral: Wikipedia catalogs them here. There is no general formula for finding the area of any quadrilateral although there is a general approach to doing so, namely,
(a) divide the quadrilateral into smaller shapes whose areas can be determined and then
(b) add up those areas.
Two Web videos that illustrate this approach are:
(1) Area of a quadrilateral on a grid from the Khan Academy
(2) Area of Complex Quadrilateral Figures from MissLichtle

We'll take up the next CCC script, "Another Great Science Calculator", in the following entry.

Comments: Post a Comment

<< Home

Powered by Blogger

Actually, reptile7's JavaScript blog is powered by Café La Llave. ;-)