Size a Triangle in place.

Blitz3D Forums/Blitz3D Programming/Size a Triangle in place.

TeraBit(Posted 2003) [#1]
Hi All,

I have a bunch of unwelded triangles scattered across a 2D plane (i.e they all have three verts to themselves and differ in VertexX and VertexY positions, but all are aligned to a Z value of 0.

What I wanted to do was to increase their size without moving their orientation or center point.

I guess it would involve working out a 2D averaged normal of the two sides connected to a vertex and moving the vertex according to this vector.

Any ideas?


Rob(Posted 2003) [#2]
I'm sure I saw a function in code archives that did exactly this...


MSW(Posted 2003) [#3]
Get each verticy X,Y, and Z values for the triangle:

X1# = first verticy X location
Y1# = first verticy Y location
Z1# = first verticy Z location

X2# = second verticy X location
Y2# = second verticy Y location
Z2# = second verticy Z location

X3# = third verticy X location
Y3# = third verticy Y location
Z3# = third verticy Z location

Then average each together:

CenterX# = (X1+X2+X3)/3
CenterY# = (Y1+Y2+Y3)/3
CenterZ# = (Z1+Z2+Z3)/3

Then find the tanget space values for these verticies:

TanX1# = X1 - CenterX
TanY1# = Y1 - CenterY
TanZ1# = Z1 - CenterZ

TanX2# = X2 - CenterX
TanY2# = Y2 - CenterY
TanZ2# = Z2 - CenterZ

TanX3# = X3 - CenterX
TanY3# = Y3 - CenterY
TanZ3# = Z3 - CenterZ

Then apply your scaleing factor (1 = same size, .5 = half size, 2 = twice the size, etc..)

TanX1 = TanX1 * scale
TanY1 = TanY1 * scale
TanZ1 = TanZ1 * scale

TanX2 = TanX2 * scale
TanY2 = TanY2 * scale
TanZ2 = TanZ2 * scale

TanX3 = TanX3 * scale
TanY3 = TanY3 * scale
TanZ3 = TanZ3 * scale

Then apply this to the triangle verticies...you do this by transforming (adding) the scaled tangent space X,Y,Z to the center X,Y,Z...which gives you the location the original verticies should be located at

First verticy new X = TanX1 + CenterX
First verticy new Y = TanY1 + CenterY
First verticy new Z = TanZ1 + CenterZ

Second verticy new X = TanX2 + CenterX
Second verticy new Y = TanY2 + CenterY
Second verticy new Z = TanZ2 + CenterZ

Third verticy new X = TanX3 + CenterX
Third verticy new Y = TanY3 + CenterY
Third verticy new Z = TanZ3 + CenterZ


pretty simple...


TeraBit(Posted 2003) [#4]
@ Rob - Can't find it. Some interesting code to find the center of a triangle though.

@MSW - I see. Although I only need X and Y as Z is constant.


Ross C(Posted 2003) [#5]
Hey Lee, how about this. Press left and right key to scale.

Graphics3D 800,600

SetBuffer BackBuffer()

Global camera=CreateCamera()
PositionEntity camera,0,0,-10

light=CreateLight()

Global mesh=CreateMesh()

Global surf=CreateSurface(mesh)

v0=AddVertex(surf,0, 1,0)
v1=AddVertex(surf,-3,0,0)
v2=AddVertex(surf,1, 0,0)

tri=AddTriangle(surf,v0,v2,v1)

Dim p#(2,2); for scaling the vertexs

Global scale#=1.0


While Not KeyHit(1)


	If KeyDown(203) Then scale=1.01:scale_triangle(surf,0,scale); set scale to either just over 1 or just below 1.
	If KeyDown(205) Then scale=0.99:scale_triangle(surf,0,scale)
	
	
	UpdateWorld
	RenderWorld
	Text 0,0,"scale="+scale
	Flip
Wend
End

Function scale_triangle(surface,index,t_scale#)
	For loop=0 To 2; loop through vertexs x and y and store them
		p(loop,0)=VertexX(surface,index+loop)
		p(loop,1)=VertexY(surface,index+loop)
	Next

	
	centre_x#= (p(0,0)+p(1,0)+p(2,0))/3; centre calculated by averaging all three points
	centre_y#= (p(0,1)+p(1,1)+p(2,1))/3; same as above
	
	; multiply the difference between the centerx and the first vertex point x, by t_scale
	; then add it the centre point x. same for y
	For loop=0 To 2
		VertexCoords(surface,index+loop,centre_x+t_scale*(p(loop,0)-centre_x),centre_y+t_scale*(p(loop,1)-centre_y),p(loop,2))
	Next
End Function




TeraBit(Posted 2003) [#6]
Hi Ross C,

Yes! Simple and effective. :) Thanks.

BTW: You wouldn't believe the ham handed routine I wrote to try and do the same thing! Some days things just don't flow :0

Thanks again :)


Ross C(Posted 2003) [#7]
Hehe, i know where your coming from :)