Heron’s Formula is a method to compute the area of a triangle when you know its three sides, a, b, and c.

The area S is then based on p where 

and can be easily implemented by following Python code.
#!/usr/bin/env python
from math import sqrt
def area(a, b, c):
p = 0.5 * (a + b + c)
return sqrt(p * (p - a) * (p - b) * (p - c))
Heron’s Formula can also be written in the following forms.



The following Delphi implements the computation via inline assembly. The FPU (Floating Point Unit) assembly is based on a stack, where you push, pop the numbers to a floating stack and conduct the computation. The single size consumes 4 byte where you should use dword to address the register while the double float number takes twice as large as single. In this case, qword is required. The following eliminates the fwait instruction and is faster, where the CPU checks for and handle pending, unmasked, floating-point exceptions before proceeding. (FWAIT is an alternate mnemonic for the WAIT).
{$DEFINE S_FLOAT}
{$IFDEF S_FLOAT}
type
float = single;
{$ELSE}
float = double;
{$ENDIF}
function TriangleArea(const a, b, c: float): float; assembler; register;
CONST
h: float = 0.25;
//Result:=sqrt((a+b+c)*(b+c-a)*(c+a-b)*(a+b-c))*0.25;
ASM
{$IFDEF S_FLOAT} // single-size 4 bytes
FLD DWORD PTR[A]
FADD DWORD PTR[B]
FADD DWORD PTR[C] // a + b + c
FLD DWORD PTR[B]
FADD DWORD PTR[C]
FSUB DWORD PTR[A] // b + c - a
FMULP ST(1), ST (a + b + c) * (b + c - a)
FLD DWORD PTR[C]
FADD DWORD PTR[A]
FSUB DWORD PTR[B] // c + a - b
FMULP ST(1), ST // (a+b+c)*(b+c-a)*(c+a-b)
FLD DWORD PTR[A]
FADD DWORD PTR[B]
FSUB DWORD PTR[C] // a + b - c
FMULP ST(1), ST //(a+b+c)*(b+c-a)*(c+a-b)*(a+b-c)
FSQRT // sqrt((a+b+c)*(b+c-a)*(c+a-b)*(a+b-c))
FLD DWORD PTR[H]
FMULP ST(1), ST //sqrt((a+b+c)*(b+c-a)*(c+a-b)*(a+b-c))*0.25
{$ELSE} // double size-8 bytes
FLD QWORD PTR[A]
FADD QWORD PTR[B]
FADD QWORD PTR[C] // a + b + c
FLD QWORD PTR[B]
FADD QWORD PTR[C]
FSUB QWORD PTR[A] // (b + c - a)
FMULP ST(1), ST // (a+b+c)(b+c-a)
FLD QWORD PTR[C]
FADD QWORD PTR[A]
FSUB QWORD PTR[B] // c + a - b
FMULP ST(1), ST // (a+b+c)(b+c-a)(c+a-b)
FLD QWORD PTR[A]
FADD QWORD PTR[B]
FSUB QWORD PTR[C] // a+b-c
FMULP ST(1), ST//(a+b+c)*(b+c-a)*(c+a-b)*(a+b-c)
FSQRT // sqrt((a+b+c)*(b+c-a)*(c+a-b)*(a+b-c))
FLD QWORD PTR[H]
FMULP ST(1), ST //sqrt((a+b+c)*(b+c-a)*(c+a-b)*(a+b-c))*0.25
{$ENDIF}
END;
–EOF (The Ultimate Computing & Technology Blog) —
786 wordsLast Post: Codeforces: C. Beautiful Sets of Points
Next Post: Ajax Tutorial - 1: How to Send HTTP/HTTPS requests via XMLHTTP Object?