Page 1 of 1

functions for TRI and QUAD figures

Posted: Wed Apr 15, 2015 7:12 am
by Henko
Here some functions to calculate the surface and the centre of gravity (cog) of the new graphic figures TRI and QUAD.
The point coordinates are passed in the same way as in the DRAW and FILL commands.
QUAD commands work ok for both convex and concave figures, whereas the functions give good results for convex figures only.

@mr. K : perhaps the manual should mention that the points in the parameter list of the QUAD functions must be given in cyclic order (if not, the result is two separate triangles in stead of a quadrangle).

Code: Select all

' test of some tri and quad functions
'
read x1,y1,x2,y2,x3,y3,x4,y4            ' define 4 (convex) points
data 500,200,600,600,200,400,200,200
graphics ! graphics clear
fill quad x1,y1,x2,y2,x3,y3,x4,y4           ' show the quad figure
pause 2
text
print surface_quad (x1,y1,x2,y2,x3,y3,x4,y4)  ' print quad surface
centre_tri(x1,y1,x2,y2,x3,y3)        ' calculate cog of a triangle
print centre_tri.x,centre_tri.y       ' print the x & y of the cog
centre_quad(x1,y1,x2,y2,x3,y3,x4,y4)   ' calculate cog of the quad
print centre_quad.x,centre_quad.y          ' print the coordinates
end

' calculates the surface of a triangle
'
def surface_tri(x1,y1,x2,y2,x3,y3)
return abs((x2-x1)*(y3-y1)-(y2-y1)*(x3-x1))/2
end def

' calculates the centre of gravity of a triangle
' retrieve the values using centre_tri.x and centre_tri.y
'
def centre_tri(x1,y1,x2,y2,x3,y3)
x=(x1+x2+x3)/3 ! y=(y1+y2+y3)/3
end def

' calculates the surface of a quad figure
' result is valid for convex figures only
' give the 4 points in cyclic order
'
def surface_quad(x1,y1,x2,y2,x3,y3,x4,y4)
surface1=surface_tri(x1,y1,x2,y2,x3,y3)
surface2=surface_tri(x1,y1,x3,y3,x4,y4)
return surface1+surface2
end def

' calculates the centre of gravity of a quad figure
' result is valid for convex figures only
' give the 4 points in cyclic order
' retrieve the values using centre_quad.x and centre_quad.y
'
def centre_quad(x1,y1,x2,y2,x3,y3,x4,y4)
s1=surface_tri(x1,y1,x2,y2,x3,y3)
s2=surface_tri(x1,y1,x3,y3,x4,y4)
centre_tri(x1,y1,x2,y2,x3,y3) ! xc1=centre_tri.x ! yc1=centre_tri.y
centre_tri(x1,y1,x3,y3,x4,y4) ! xc2=centre_tri.x ! yc2=centre_tri.y
x=xc2+s1*(xc1-xc2)/(s1+s2)    ! y=yc2+s1*(yc1-yc2)/(s1+s2)
end def

Re: functions for TRI and QUAD figures

Posted: Wed Apr 15, 2015 7:26 am
by Mr. Kibernetik
Thank you for the note.
Yes, quadrilateral can be crossed.