# Is a point within a triangle?

The problem is a suggested Rosetta Code task- check if a random ( x, y) point lies within a triangle defined by its three corners. The reference gives three different ways of solving this.

## Code in Liberty BASIC

```    nomainwin

WindowWidth  =500
WindowHeight =500

open "Is a random point inside a triangle?" for graphics_nsb as #wg

#wg "trapclose quit"

#wg "down ; fill darkblue ; color cyan ; size 1"

x1 =200: y1 =400: #wg "set  "; x1; " "; y1
x2 =400: y2 =400: #wg "goto "; x2; " "; y2
x3 =300: y3 =100: #wg "goto "; x3; " "; y3
#wg " goto "; x1; " "; y1
#wg "flush"

N =0
call save N

do
x =int( 500 *rnd( 1)): y =int( 500 *rnd( 1))

a   = ( ( y2 -y3) *( x -x3) +( x3 -x2) *( y -y3)) / ( ( y2 -y3) *(x1 -x3) +( x3 -x2) *( y1 -y3))
b   = ( ( y3 -y1) *( x -x3) +( x1 -x3) *( y -y3)) / ( ( y2 -y3) *(x1 -x3) +( x3 -x2) *( y1 -y3))
c   = 1 -a -b

'        p lies in T if and only if 0 <= a <= 1 and 0 <= b <= 1 and 0 <= c <= 1
if      ( ( 0 <=a) and ( a <=1))_
and ( ( 0 <=b) and ( b <=1))_
and ( ( 0 <=c) and ( c <=1))_
then
#wg "color green"
else
#wg "color red"
end if
#wg "set "; x ; " "; y
scan
N   =N +1
if ( N mod 10000) =0 then call save N
loop until 0

sub quit h\$
close #h\$
end
end sub

sub save N
#wg "getbmp scr 1 1 500 500"
'bmpsave "scr", "triangle" +right\$( "000000" +str\$( N), 6) +".bmp"
#wg "cls"
#wg "drawbmp scr 1 1"
end sub

```