Par exemple, avoir un axe de rotation (axe_x,axe_y) et un triangle (ax,ay)(bx,by)(cx,cy).
Je n'arrive pas a trouver la formule qui contient des cos et des sin
Merci pour vos lumieres

Code : Tout sélectionner
; SPH(2021)
; PB5.73LTS
InitSprite()
InitMouse()
InitKeyboard()
;SetPriorityClass_ ( GetCurrentProcess_ (), #IDLE_PRIORITY_CLASS )
If ExamineDesktops()
dw.w=DesktopWidth(0)
dh.w=DesktopHeight(0)
Else
dw.w=1024
dh.w=768
EndIf
dw2=dw/2
dh2=dh/2
OpenScreen(dw,dh,32,"")
;;;;;;;;;;;
;;;;;;;;;;;
xx=dw2
yy=dh2
xf.f=0
yf.f=0
;;;;;;;;;;;
;;;;;;;;;;;
Repeat ; Until xmouse<>xmouse2 Or ymouse<>ymouse2 Or KeyboardPushed(#PB_Key_All)
;*****
;ClearScreen(0)
StartDrawing(ScreenOutput())
x1=(xx-150)
y1=(yy-280)
x2=(xx+333)
y2=(yy-240)
x3=(xx-700)
y3=(yy+40)
LineXY(x1,y1,x2,y2,RGB(255,255,255))
LineXY(x1,y1,x3,y3,RGB(255,255,255))
LineXY(x2,y2,x3,y3,RGB(255,255,255))
; ; ; ; ; ; ; ; ; ; new_x = (old_x - dw2) * Cos(angle) - (old_y - dh2) * Sin(angle)
; ; ; ; ; ; ; ; ; ; new_y = (old_x - dw2) * Sin(angle) + (old_y - dh2) * Cos(angle)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Line(new_x,new_y,5,5,RGB(255,155,55));;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
xf+0.01
yf+0.01
;;;;;;; red cross (axis x and y)
LineXY(dw2-2,dh2-2,dw2+2,dh2+2,RGB(255,0,0))
LineXY(dw2+2,dh2-2,dw2-2,dh2+2,RGB(255,0,0))
;LineXY(dw2-rr,i,dw2+vv,i,RGB(r1+r2,v1+v2,b1+b2))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
StopDrawing()
;*****
FlipBuffers()
ExamineKeyboard()
Until KeyboardPushed(#PB_Key_All)
End
Code : Tout sélectionner
; =============================================================================
;
; =============================================================================
#MATH_EPSILON = 1e-03
#USE_DOUBLE_PRECISION = #False
; =============================================================================
;
; =============================================================================
CompilerIf #USE_DOUBLE_PRECISION = #True
Macro real:d:EndMacro
Macro PokeReal(_B_,_V_):PokeD(_B_,_V_):EndMacro
Macro PeekReal(_B_):PeekD(_B_):EndMacro
CompilerElse
Macro real:f:EndMacro
Macro PokeReal(_B_,_V_):PokeF(_B_,_V_):EndMacro
Macro PeekReal(_B_):PeekF(_B_):EndMacro
CompilerEndIf
Structure Matrix4
m.real[16]
EndStructure
; =============================================================================
;
; =============================================================================
Procedure Matrix4_set(*self.Matrix4, a00.real, a01.real, a02.real, a10.real, a11.real, a12.real, a20.real, a21.real, a22.real)
With *self
\m[0] = a00 : \m[4] = a01 : \m[8] = 0.0 : \m[12] = a02
\m[1] = a10 : \m[5] = a11 : \m[9] = 0.0 : \m[13] = a12
\m[2] = 0.0 : \m[6] = 0.0 : \m[10] = 1.0 : \m[14] = 0.0
\m[3] = a20 : \m[7] = a21 : \m[11] = 0.0 : \m[15] = a22
EndWith
EndProcedure
; =============================================================================
;
; =============================================================================
Procedure Matrix4_transform_point(*self.Matrix4, *x , *y)
With *self
_x.real = PeekReal(*x)
_y.real = PeekReal(*y)
t_x.real = _x
t_y.real = _y
PokeReal(*x, \m[0] * t_x + \m[4] * t_y + \m[12])
PokeReal(*y, \m[1] * t_x + \m[5] * t_y + \m[13])
EndWith
EndProcedure
Structure Vector2
x.real
y.real
EndStructure
Structure Polygone
origin.Vector2
position.Vector2
scale.Vector2
transform.Matrix4
rotation.real
need_update.b
List vertex.Vector2()
EndStructure
Procedure Add_vertex(*self.Polygone, x.real, y.real)
AddElement(*self\vertex())
*self\vertex()\x = x
*self\vertex()\y = y
EndProcedure
; =============================================================================
;
; =============================================================================
Procedure.i Get_transform(*self.Polygone)
If *self\need_update
angle.real = -*self\rotation * #PI / 180.0
cosine.real = Cos(angle)
sine.real = Sin(angle)
sxc.real = *self\scale\x * cosine
syc.real = *self\scale\y * cosine
sxs.real = *self\scale\x * sine
sys.real = *self\scale\y * sine
tx.real = -*self\origin\x * sxc - *self\origin\y * sys + *self\position\x
ty.real = *self\origin\x * sxs - *self\origin\y * syc + *self\position\y
Matrix4_set(*self\transform,sxc, sys, tx, -sxs, syc, ty, 0.0,0.0,1.0)
*self\need_update = #False
EndIf
ProcedureReturn *self\transform
EndProcedure
Procedure Render_polygon(*self.Polygone)
*transform = Get_transform(*self )
FirstElement(*self\vertex())
ForEach *self\vertex()
*A.Vector2 = *self\vertex()
PushListPosition(*self\vertex())
If NextElement(*self\vertex()) <> 0
*B.Vector2 = *self\vertex()
Else
FirstElement(*self\vertex())
*B.Vector2 = *self\vertex()
EndIf
PopListPosition(*self\vertex())
x1.real = *A\x
y1.real = *A\y
x2.real = *B\x
y2.real = *B\y
Matrix4_transform_point(*transform,@x1,@y1)
Matrix4_transform_point(*transform,@x2,@y2)
LineXY(x1,y1,x2,y2,0)
; LineXY(*A\x,*A\y,*B\x,*B\y,0)
Next
; End
EndProcedure
;EXEMPLE :
OpenWindow(0,0,0,1650,1080,"")
canvas = CanvasGadget(#PB_Any,0,0,1650,1080)
*Triangle.Polygone = AllocateStructure(Polygone)
*Triangle\need_update = #True
*Triangle\scale\x = 1 ; TRES IMPORTANT , SINON TU VOIS RIEN !
*Triangle\scale\y = 1
; ON AJOUTE LES VERTEX
Add_vertex(*Triangle, 0,0)
Add_vertex(*Triangle, 100,0)
Add_vertex(*Triangle, 100,100)
; ON CHANGE L'ORIGINE
*Triangle\origin\x = 50
*Triangle\origin\y = 50
; LA POSITION
*Triangle\position\x = 1650/2
*Triangle\position\y = 1080/2
delta_time.f = 0
While #True
delta_clock.l = ElapsedMilliseconds()
Repeat
event = WindowEvent()
If event = #PB_Event_CloseWindow
Break 2
EndIf
Until event = 0
*Triangle\rotation + 90 * delta_time
*Triangle\need_update = #True ; TRES IMPORTANT AUSSI, SINON IL NE TOURNE PAS
StartDrawing(CanvasOutput(canvas))
Box(0,0,OutputWidth(), OutputHeight(),$FFFFFF)
Render_polygon(*Triangle)
StopDrawing()
delta_time = (ElapsedMilliseconds()-delta_clock) / 1000
Wend