Using Square Root to calculate Area

Started by
19 comments, last by taby 13 years, 8 months ago
To calculate the area of a 2D square or rectangle, you multiply the width by height. However, unless I then get the square root of this value the result is wrong!

    Public Overrides Function GetArea() As Single        Return Math.Sqrt(ShapeWidth * ShapeHeight)    End Function


Why do I need to apply the square root?
Advertisement
Could you provide a testcase and show some example outputs?
Because
ShapeWidth * ShapeHeight is NOT Sqrt(ShapeWidth * ShapeHeight)

remain :
Sqrt(x) * Sqrt(x) = x
Have a great day!Richard Geslot, 3D game developermy 3D creation
Why do you think that the value is wrong? That is the correct calculation for the area of a rectangle.
Hmm ... does it have something to do with my coordinates? Perhaps related, why would I also need to square root the result of a distance calculation?

    Public Function Distance(ByVal Point1 As PointF, ByVal Point2 As PointF) As Single        Dim dx As Single = Point1.X - Point2.X        Dim dy As Single = Point1.Y - Point2.Y        Return Math.Sqrt(dx * dx + dy * dy)    End Function
Well this is just the application of the Pythagorean theorem to calculate the distance between two points (in euclidian co-ordinates to be precise). And no, it's not (directly) related to your rectangle-area-calculation in your original post.
Work it out with some graph paper. For a 2x2 square the area is 4, two times two is four, your code is wrong.
"It's like naming him Asskicker Monstertrucktits O'Ninja" -Khaiy

OK the Area of a 4 sided figure, Square or Rectangle is Width * height.

If you are using coordinates you need to calculate the distance of the width and the height.

IE:

Width will be W = Sqrt((X2-X1)^2 + (Y2-Y1)^2)
Height will be H = Sqrt((X3-X1)^2 + (Y3-Y1)^2)

So area is W * H

OR

Sqrt((X2-X1)^2 + (Y2-Y1)^2) * Sqrt((X3-X1)^2 + (Y3-Y1)^2)

A more robust method that will work for any closed figure is to calculate by sum of latitudes and departures : http://www.wikihow.com/Calculate-the-Area-of-a-Polygon
Quote:Original post by Magpie
OK the Area of a 4 sided figure, Square or Rectangle is Width * height.

If you are using coordinates you need to calculate the distance of the width and the height.

IE:

Width will be W = Sqrt((X2-X1)^2 + (Y2-Y1)^2)
Height will be H = Sqrt((X3-X1)^2 + (Y3-Y1)^2)

So area is W * H

OR

Sqrt((X2-X1)^2 + (Y2-Y1)^2) * Sqrt((X3-X1)^2 + (Y3-Y1)^2)

You don't need to do it that way, but you certainly can.

Quote:A more robust method that will work for any closed figure is to calculate by sum of latitudes and departures : http://www.wikihow.com/Calculate-the-Area-of-a-Polygon

Yes, that's the method I would use, although that page doesn't explain at all why it works.
Quote:Original post by Magpie
OK the Area of a 4 sided figure, Square or Rectangle is Width * height.

If you are using coordinates you need to calculate the distance of the width and the height.

IE:

Width will be W = Sqrt((X2-X1)^2 + (Y2-Y1)^2)
Height will be H = Sqrt((X3-X1)^2 + (Y3-Y1)^2)

So area is W * H


Ahaha - Yes I am using a 2D coord system. This must be why I need the square root in there, because it needs to be the distance otherwise it doesn't make sense when I convert it to real world measurements.
I can expand my actual function - this is what it is doing:

Area = Math.Sqrt((m_MaxPoint.X - m_MinPoint.X) * (m_MaxPoint.Y - m_MinPoint.Y))

Does that sit with what you have said Magpie?


This topic is closed to new replies.

Advertisement