Показать сообщение отдельно
Старый 14.10.2013, 19:39   #9
polopok
ПроЭктировщик
 
Регистрация: 17.07.2009
Сообщений: 182
Написано 51 полезных сообщений
(для 71 пользователей)
Ответ: некоторые изометрические демки

Думаю немного наглядных примеров действий с векторами под блитз ,не помешаеют.
Статья донор здесь и здесь , а так же там много разных примеров .

; наглядное изображение векторов 

Type Vector
	Field X# , Y# , L# , vX# , vY#
End Type
	
Graphics 800,600,32,2 
SetBuffer BackBuffer() 
ArialCyrFont = LoadFont ("Arial cyr",16)
SetFont ArialCyrFont 
qx1# = 200 : qy1# = 200
qx2# = 220 : qy2# = 350
F.Vector= Vector(qx1,qy1,qx2,qy2)
VectorNormalize(F)
F1.Vector= VectorClone(F)
F2.Vector= VectorClone(F)
F3.Vector= VectorClone(F)

VectorLeft(F2)
VectorRight(F1)
VectorReverse(F3)
While Not KeyHit(1) 
mx#  = MouseX() : my# = MouseY()
Cls 

	Color 120,120,120
	If Dist( qx1,qy1,mx,my)<10 And MouseDown(1) 
		qx1= mx : qy1=my
		Oval mx-10,my-10, 20,20,0	
		
	Else 
		Rect  qx1-5,qy1-5, 10,10,0	
	EndIf 
	If Dist( qx2,qy2,mx,my)<10  And MouseDown(1) 
		qx2= mx : qy2=my
		Oval mx-10,my-10, 20,20,0	
		VectorReverseClone(F3,F)
	Else 
		Rect  qx2-5,qy2-5, 10,10,0
	EndIf
	
	VectorUpdata(F, qx1,qy1,qx2,qy2)
	VectorCloneUpdata(F1 , F)
	VectorCloneUpdata(F2 , F)
	VectorCloneUpdata(F3 , F)
	VectorLeft(F2)
	VectorRight(F1)	
	VectorReverseClone(F3,F)
	
	


VectorDraw(F,qx1,qy1,255,0,0)
VectorDraw(F1,qx1,qy1,0,0,255)
VectorDraw(F2,qx1,qy1,0,255,0)
VectorDraw(F3,qx1,qy1,150,150,150)

Text 20,20,"Навести мышь на квадрат и удерживая нажатой левую кнопку мыши, затем "
Text 20,40,"переместить стрелку мыши в новую позицию"
Flip 
Wend 
Delete Each Vector
FreeFont ArialCyrFont 
End


Function VectorDraw(a.Vector ,X1# , Y1# , R=255, G=255, B=255)

	Color R,G,B
	Line X1 ,Y1 , X1+ a\X*a\L , Y1+a\Y*a\L
	Oval X1+a\X*a\L-2,Y1+ a\Y*a\L-2,3,3,1
	Color 255,255,255
	Line X1 ,Y1 , X1+ a\X*5 , Y1+a\Y*5	
End Function	

Function VectorClone.Vector(a.Vector)
	Local v.Vector = New Vector
	v\X = a\X
	v\Y = a\Y
	v\L = a\L
	Return v
End Function

Function Vector.Vector(X1#=0,Y1#=0,X2#=0,Y2#=0)
	Local v.Vector = New Vector
	v\X = X2 -X1
	v\Y = Y2 -Y1
	v\vX = v\X
	v\vY = v\Y
	Return v
End Function

Function VectorUpdata(a.Vector , X1#=0,Y1#=0,X2#=0,Y2#=0)
	a\X = X2 -X1
	a\Y = Y2 -Y1
	a\vX = a\X
	a\vY = a\Y
	a\L = VectorMagnitude#(a)
	VectorNormalize(a)
End Function

Function VectorCloneUpdata(a.Vector , b.Vector)
	a\X =b\X
	a\Y = b\Y
	a\vX = b\vX
	a\vY = b\vY
	a\L = b\L
End Function
	
Function VectorAdd(a.Vector,b.Vector)
	a\X =a\X + b\X
	a\Y =a\Y + b\Y
End Function
	
Function VectorRight(a.Vector)
Local  cX# , cY#
	cX= -a\X  : cY = a\Y
	a\X =cY
	a\Y =cX
End Function
	
Function VectorLeft(a.Vector)
Local  cX# , cY#
	cX= a\X  : cY = -a\Y
	a\X =cY
	a\Y =cX
End Function

Function VectorReverse(a.Vector)
Local  cX# , cY#
	cX= -a\X  : cY = -a\Y
	a\X =cX
	a\Y =cY
End Function

Function VectorReverseClone(a.Vector,b.Vector)
	a\X =-b\X
	a\Y =-b\Y
End Function
	
Function VectorSubtract(a.Vector,b.Vector)
	a\X =a\X - b\X
	a\Y =a\Y - b\Y
End Function
	
Function VectorSum.Vector(a.Vector,b.Vector)
	Return Vector(a\X+b\X,a\Y+b\Y)
End Function
	
Function VectorDifference.Vector(a.Vector,b.Vector)
	Return Vector(a\X-b\X,a\Y-b\Y)
End Function
	
Function VectorMultiply(a.Vector,b.Vector)
	a\X =a\X * b\X
	a\Y =a\Y * b\Y
End Function
	
Function VectorDivide(a.Vector,b.Vector)
	a\X =a\X / b\X
	a\Y =a\Y / b\Y
End Function
	
Function VectorProduct.Vector(a.Vector,b.Vector)
	Return Vector(a\X*b\X,a\Y*b\Y)
End Function
	
Function VectorQuotient.Vector(a.Vector,b.Vector)
	Return Vector(a\X/b\X,a\Y/b\Y)
End Function

Function VectorCross(a.Vector,b.Vector)
	Return  (a\X*b\Y)-(a\Y*b\X) 
End Function	

Function VectorDot#(a.Vector,b.Vector)
	Return (a\X*b\X) + (a\Y*b\Y)
End Function
	
Function VectorAngle#(a.Vector,b.Vector)
	Local d# = VectorDot(a,b)
	Local m# = VectorMagnitude(a)*VectorMagnitude(b)
	Return ACos(d#/m#)
End Function
	
Function VectorNormalize(a.Vector)
	Local m# = VectorMagnitude(a)
	If m<= 0.0001Then m=1
	a\X = a\X / m#
	a\Y = a\Y / m#
End Function
	
Function VectorMagnitude#(a.Vector)
	a\L = Sqr(a\X*a\X + a\Y*a\Y)
	Return a\L
End Function

Function Dist#( X1#, Y1#, X2#, Y2# )
	Return Abs(( (X1 - X2)*(X1 - X2) + (Y1 - Y2)*(Y1 - Y2) )^0.5)
End Function

; наглядное изображение векторов 

Type Vector
	Field X# , Y# , L# , vX# , vY#
	Field px# , py# ,px2# , py2#
End Type
Global qx1#,qy1#
	
Graphics 800,600,32,2 
SetBuffer BackBuffer() 
ArialCyrFont = LoadFont ("Arial cyr",16)
SetFont ArialCyrFont 
qx1# = 300 :qx2# = 290 
qy1# = 50 : qy2# = 120

F.Vector= Vector(qx1,qy1,qx2,qy2)
;VectorNormalize(F)
F1.Vector= Vector(250,250,400,200)
VectorNormalize(F1)
F2.Vector= Vector(qx1,qy1,250,250)
;VectorNormalize(F2)
While Not KeyHit(1) 
mx#  = MouseX() : my# = MouseY()
Cls 

	Color 120,120,120
	If Dist( qx1,qy1,mx,my)<10 And MouseDown(1) 
		qx1= mx : qy1=my
		Oval mx-10,my-10, 20,20,0			
	Else 
		Rect  qx1-5,qy1-5, 10,10,0	
	EndIf 
	If Dist( qx2,qy2,mx,my)<10  And MouseDown(1) 
		qx2= mx : qy2=my
		Oval mx-10,my-10, 20,20,0	
	Else 
		Rect  qx2-5,qy2-5, 10,10,0
	EndIf

	VectorUpdata(F, qx1,qy1,qx2,qy2)
	VectorUpdata(F2, qx1,qy1,250,250)
	t# = FindIntersectionVectors#(F,F1, F2); 
	fx# = FindX#(F1,t#)
	fy# = FindY#(F1,t#)
	Rect fx-5,fy-5,10,10,1
	Color 120,0,0
	Rect fx-5,fy-5,10,10,0
VectorDraw(F,255,0,0)
VectorDraw(F1,0,0,255)
VectorDraw(F2,0,255,0)

Text 20,20,"Навести мышь на квадрат и удерживая нажатой левую кнопку мыши, затем "
Text 20,40,"переместить стрелку мыши в новую позицию"
Text 20,60,"VectorMagnitude : "+t
Flip 
Wend 
Delete Each Vector
FreeFont ArialCyrFont 
End


Function VectorDraw(a.Vector , R=255, G=255, B=255)

	Color R,G,B
	Line a\px ,a\py , a\px+ a\X*a\L , a\py+a\Y*a\L
	Oval a\px+a\X*a\L-2,a\py+ a\Y*a\L-2,3,3,1
	Color 255,255,255
	Line a\px,a\py , a\px+ a\X*5 , a\py+a\Y*5	
	Oval a\px+a\X*a\L-3,a\py+ a\Y*a\L-3,5,5,0
End Function	

Function VectorClone.Vector(a.Vector)
	Local v.Vector = New Vector
	v\X = a\X
	v\Y = a\Y
	v\L = a\L
	v\px = a\px
	v\py = a\py
	v\px2 = a\px2
	v\py2 = a\py2
	Return v
End Function

Function Vector.Vector(X1#=0,Y1#=0,X2#=0,Y2#=0)
	Local v.Vector = New Vector
	v\X = X2 -X1
	v\Y = Y2 -Y1
	v\vX = v\X
	v\vY = v\Y
	v\px =X1
	v\py = Y1
	v\px2 =X2
	v\py2 = Y2
	Return v
End Function

Function VectorUpdata(a.Vector , X1#=0,Y1#=0,X2#=0,Y2#=0)
	a\X = X2 -X1
	a\Y = Y2 -Y1
	a\vX = X2 -X1
	a\vY = Y2 -Y1
	a\px = X1
	a\py = Y1
	a\L = VectorMagnitude#(a)
	VectorNormalize(a)
End Function

Function	FindIntersectionVectors#(a.Vector,b.Vector, c.Vector); 
	Local t#
	
	VectorUpdata(c, a\px,a\py,b\px,b\py)
	If ((a\x = b\x And a\y = b\y) Or (a\x = -a\x And a\y = -b\y)) 
		t = 1000000
	 Else 
		 t# =(c\vx*a\vy-c\vy*a\vx)/(a\vx*b\vy-a\vy*b\vx)
	EndIf
	Return t
End Function	

Function FindX#(a.Vector,tt#)
	Return a\px + a\vx*tt
End Function

Function FindY#(a.Vector,tt#)
	Return  a\py + a\vy*tt
End Function

Function VectorCloneUpdata(a.Vector , b.Vector)
	a\X =b\X
	a\Y = b\Y
	a\vX = b\vX
	a\vY = b\vY
	a\L = b\L
End Function
	
Function VectorAdd(a.Vector,b.Vector)
	a\X =a\X + b\X
	a\Y =a\Y + b\Y
End Function
	
Function VectorRight(a.Vector)
Local  cX# , cY#
	cX= -a\X  : cY = a\Y
	a\X =cY
	a\Y =cX
End Function
	
Function VectorLeft(a.Vector)
Local  cX# , cY#
	cX= a\X  : cY = -a\Y
	a\X =cY
	a\Y =cX
End Function

Function VectorReverse(a.Vector)
Local  cX# , cY#
	cX= -a\X  : cY = -a\Y
	a\X =cX
	a\Y =cY
End Function

Function VectorReverseClone(a.Vector,b.Vector)
	a\X =-b\X
	a\Y =-b\Y
End Function
	
Function VectorSubtract(a.Vector,b.Vector)
	a\X =a\X - b\X
	a\Y =a\Y - b\Y
End Function
	
Function VectorSum.Vector(a.Vector,b.Vector)
	Return Vector(a\X+b\X,a\Y+b\Y)
End Function
	
Function VectorDifference.Vector(a.Vector,b.Vector)
	Return Vector(a\X-b\X,a\Y-b\Y)
End Function
	
Function VectorMultiply(a.Vector,b.Vector)
	a\X =a\X * b\X
	a\Y =a\Y * b\Y
End Function
	
Function VectorDivide(a.Vector,b.Vector)
	a\X =a\X / b\X
	a\Y =a\Y / b\Y
End Function
	
Function VectorProduct.Vector(a.Vector,b.Vector)
	Return Vector(a\X*b\X,a\Y*b\Y)
End Function
	
Function VectorQuotient.Vector(a.Vector,b.Vector)
	Return Vector(a\X/b\X,a\Y/b\Y)
End Function

Function VectorCross#(a.Vector,b.Vector)
	Return (a\X*b\Y)-(a\Y*b\X) 
End Function	


Function VectorDot#(a.Vector,b.Vector)
	Return (a\X*b\X) + (a\Y*b\Y)
End Function
	
Function VectorAngle#(a.Vector,b.Vector)
	Local d# = VectorDot(a,b)
	Local m# = VectorMagnitude(a)*VectorMagnitude(b)
	Return ACos(d#/m#)
End Function
	
Function VectorNormalize(a.Vector)
	Local m# = VectorMagnitude(a)
	If m<= 0.0001Then m=1
	a\X = a\X / m#
	a\Y = a\Y / m#
End Function
	
Function VectorMagnitude#(a.Vector)
	a\L = Sqr(a\X*a\X + a\Y*a\Y)
	Return a\L
End Function

Function Dist#( X1#, Y1#, X2#, Y2# )
	Return Abs(( (X1 - X2)*(X1 - X2) + (Y1 - Y2)*(Y1 - Y2) )^0.5)
End Function


Конечно ,лучше оформить отдельный ББ файл с функциями операций над векторами и просто присоединять его к основной программе.
__________________
Мой проект здесь
(Offline)
 
Ответить с цитированием