[futurebasic] Re: [FB] Challenge (of sorts)

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : May 2001 : Group Archive : Group : All Groups

From: Robert Purves <robert.purves@...>
Date: Fri, 4 May 2001 12:32:14 +1200
>>Given any triangle, and the 3 vertices each being assigned a value of 1.0
>>using  (v1,v2,v3) order, come up with a FN that gives the distance of any
>>point within the triangle from the 3 vertices measured as a fraction of
>>those values.
>>
>>Point in exact middle, (.5,.5,.5)
>>
>>Point more toward v1 for example, (.6,.3,.4) all depending on triangle shape.
>>
>>The "attribute" I mention could be thought of as the distance the point is
>>from each vertex in a manner of speaking.
>>
>>Right at vertex v1, then the attribute would be (1.0,0,0)


>The 1.0 refers to color values assigned to each vertex, not the x,y
>coordinates, in case that was a point of confusion.


The program below seems to meet your specification, with some changes to
make the sum of weights = 1.0. I don't think it will be much good in a
Gouraud shading context, though. You will end up with colour
discontinuities between adjacent triangles.

Robert P.

'---complete FB^3 program---
end globals

begin record weight
dim as double  w0, w1, w2
end record

begin record point2D
dim as double  x, y
end record

// Distance between 2 points
local
dim as double  dx, dy
local fn Delta#( pointA as ^point2D, pointB as ^point2D )
dx = pointA.x - pointB.x
dy = pointA.y - pointB.y
end fn = sqr( dx*dx + dy*dy )


// Weight by closeness of each vertex to thePoint
local
dim as double dz0, dz1, dz2, c0, c1, c2, sum
local fn PointLoc( thePoint as ^point2D, ¬
                 vertex0  as ^point2D, ¬
                 vertex1  as ^point2D, ¬
                 vertex2  as ^point2D, ¬
                 weight   as ^weight )
dz0 = fn Delta#( thePoint, vertex0 )
dz1 = fn Delta#( thePoint, vertex1 )
dz2 = fn Delta#( thePoint, vertex2 )
if dz0 != 0.0 then c0 = (dz1 + dz2)/dz0 else c0 = 1e300
if dz1 != 0.0 then c1 = (dz0 + dz2)/dz1 else c1 = 1e300
if dz2 != 0.0 then c2 = (dz1 + dz0)/dz2 else c2 = 1e300
// make sum of weights 1.0
sum = c0 + c1 + c2
weight.w0 = c0/sum
weight.w1 = c1/sum
weight.w2 = c2/sum
end fn

// Demo program
// Move mouse to see "closeness" to the triangle vertices
dim as point2D      vertex0, vertex1, vertex2
dim as point2D      testPoint
dim as weight       pointWt

dim as point        mousePt

_xOrigin = 100
_yOrigin =  50
_side    = 300

window 1

vertex0.x = _xOrigin
vertex0.y = _yOrigin

vertex1.x = _xOrigin + _side
vertex1.y = _yOrigin

vertex2.x = _xOrigin + _side*0.5
vertex2.y = _yOrigin + _side*0.5

MoveTo( vertex0.x, vertex0.y )
LineTo( vertex1.x, vertex1.y )
LineTo( vertex2.x, vertex2.y )
LineTo( vertex0.x, vertex0.y )

do
GetMouse( mousePt )
testPoint.x = mousePt.h%
testPoint.y =  mousePt.v%
fn PointLoc( testPoint, vertex0, vertex1, vertex2,  pointWt )
TextMode( _srcCopy )
print @(0, 15)
print using "##.#######"; pointWt.w0
print using "##.#######"; pointWt.w1
print using "##.#######"; pointWt.w2
handleevents
until fn Button
'---------------------