Date: 02-16-2022
Return to Index
created by gbSnippets
'sinx ~ x-x^3/6 and cosx~1-x^2/4
' D(x,y).x = XCenter + (x - XCenter) * Cos(theta) - (y - YCenter) * Sin(theta)
' D(x,y).y = YCenter + (x - XCenter) * Sin(theta) + (y - YCenter) * Cos(theta)
new_x = old_x * cos(theta) - old_y * sin(theta)
new_y = old_x * sin(theta) + old_y * cos(theta)
new_x = pivot_x + (old_x - pivot_x) * cos(theta) - (old_y - pivot_y) * sin(theta)
new_y = pivot_y + (old_x - pivot_x) * sin(theta) + (old_y - pivot_y) * cos(theta)
Rotating a point from position (o.x, o.y) to position (oR.x, oR.y) through an angle "theta" about pivot point (oP.x, oP.y)
oR.x = oP.x + (o.x - oP.x) * cos(theta) - (o.y - oP.y) * sin(theta)
oR.y = oP.y + (o.x - oP.x) * sin(theta) + (o.y - oP.y) * cos(theta) `
%IDC_Button4 = 508
Control Add Button, hDlg, %IDC_Button4,"4", 70,100,50,20
Sub RotateImage_GetPixelD
Local pOld, w,h,x,y,iCount, XCenter,YCenter As Long, sintheta, costheta As Single, bmp$, P As Byte PTR
Dim D(200,200) As gbPoint
XCenter = 100 : YCenter = 100
sintheta = sin(theta) : costheta = cos(theta)
Graphic Attach hDlg, %IDC_Graphic, Redraw
Graphic Render "cowgirl", (50,50)-(149,149) 'same size (could resize)
Graphic Get Bits To bmp$
w = CVL(bmp$,1) : h = CVL(bmp$,5)
For x = 50 to 149
For y = 50 to 149
D(x,y).x = XCenter + (x - XCenter) * Costheta - (y - YCenter) * Sintheta
D(x,y).y = YCenter + (x - XCenter) * Sintheta + (y - YCenter) * Costheta
D(x,y).Col = CVL(bmp$, (y*w+x)*4+8 )
Next y
Next x
Graphic Clear : Graphic Get Bits To bmp$
p = StrPTR(bmp$)+8 'position of starting position for bits in string
pOld = p 'save pointer starting position to use later
For x = 50 to 149
For y = 50 to 149
Mid$(bmp$,(D(x,y).y * w + D(x,y).x)*4+8,4) = Mkl$(D(x,y).col)
' p = pOld + (D(x,y).y * w + D(x,y).x)*4
' Poke$ p, Mkl$(D(x,y).col)
' @p = Mkl$(BGR(D(x,y).col))
Next y
Next x
Graphic Set Bits bmp$ : Graphic Redraw
End Sub
Case %IDC_Button4 : Tix T : RotateImage_GetPixelD : Tix End T : Control Set Text hDlg, %IDC_Label, Format$(T, "###,###,###")
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Resource "gbsnippets.pbr"
Type gbPoint
x As Long 'new x position
y As Long 'new y position
col As Long 'color of pixel
End Type
%IDC_Graphic = 501
%IDC_Source = 502
%IDC_Label = 503
%IDC_Button0 = 504
%IDC_Button1 = 505
%IDC_Button2 = 506
%IDC_Button3 = 507
Global hDlg As Dword, theta As Single
Function PBMain() As Long
Dialog New Pixels, 0, "Rotate Image",300,300,375,300, %WS_SysMenu, 0 To hDlg
Control Add Label, hDlg, %IDC_Label, "<timer>", 10,10,125,20
Control Add Button, hDlg, %IDC_Button0,"Reset", 10,40,50,20
Control Add Button, hDlg, %IDC_Button1,"1", 10,70,50,20
Control Add Button, hDlg, %IDC_Button2,"2", 10,100,50,20
Control Add Button, hDlg, %IDC_Button3,"3", 70,70,50,20
Control Add Graphic, hDlg, %IDC_Source,"", 10,140,100,100, %WS_Border
Control Add Graphic, hDlg, %IDC_Graphic,"", 150,10,200,200, %WS_Border
Graphic Attach hDlg, %IDC_Source
Graphic Render "cowgirl", (0,0)-(99,99) 'same size (could resize)
Graphic Attach hDlg, %IDC_Graphic
Graphic Render "cowgirl", (50,50)-(149,149) 'same size (could resize)
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local T as Quad, i As Long
Select Case Cb.Msg
Case %WM_InitDialog
theta = 0.1
Case %WM_MouseWheel
Select Case Hi(Integer,Cb.WParam) 'note the use of Integer
Case > 0 : theta = theta + 0.2 : RotateImage_GetPixelC
Case < 0 : theta = theta - 0.2 : RotateImage_GetPixelC
End Select
Case %WM_Command
Select Case Cb.Ctl
Case %IDC_Button0 : Graphic Attach hDlg, %IDC_Graphic : Graphic Clear
Graphic Render "cowgirl", (50,50)-(149,149) 'same size (could resize)
Case %IDC_Button1 : Tix T : RotateImage_GetPixelA : Tix End T : Control Set Text hDlg, %IDC_Label, Format$(T, "###,###,###")
Case %IDC_Button2 : Tix T : RotateImage_GetPixelB : Tix End T : Control Set Text hDlg, %IDC_Label, Format$(T, "###,###,###")
Case %IDC_Button3 : Tix T : RotateImage_GetPixelC : Tix End T : Control Set Text hDlg, %IDC_Label, Format$(T, "###,###,###")
End Select
End Select
End Function
Sub RotateImage_GetPixelA
Local c,w,h,x,y, iCount As Long, po, por, pop As PointAPI
Dim D(99,99) As gbPoint
theta = 0.5 : pop.x = 49 : pop.y = 49 'point of rotation
Graphic Attach hDlg, %IDC_Graphic, Redraw : Graphic Clear
For x = 0 To 99 : For y = 0 To 99
po.x = x : po.y = y
'Rotating a point from position (o.x, o.y) to position (oR.x, oR.y) through an angle "theta" about pivot point (oP.x, oP.y)
poR.x = poP.x + (po.x - poP.x) * Cos(theta) - (po.y - poP.y) * Sin(theta)
poR.y = poP.y + (po.x - poP.x) * Sin(theta) + (po.y - poP.y) * Cos(theta)
Graphic Attach hDlg, %IDC_Source, Redraw
Graphic Get Pixel (po.x,po.y) To c
Graphic Attach hDlg, %IDC_Graphic, Redraw
Graphic Set Pixel (poR.x+50,poR.y+50), c
Next y : Next y
Graphic Redraw
End Sub
Sub RotateImage_GetPixelB
Local c,i,j,w,h,x,y,XCenter,YCenter As Long
Dim D(200,200) As gbPoint
theta = 0.5 : XCenter = 100 : YCenter = 100
Graphic Attach hDlg, %IDC_Graphic, Redraw
For x = 50 To 149
For y = 50 To 149
D(x,y).x = XCenter + (x - XCenter) * Cos(theta) - (y - YCenter) * Sin(theta)
D(x,y).y = YCenter + (x - XCenter) * Sin(theta) + (y - YCenter) * Cos(theta)
Graphic Get Pixel (x,y) To D(x,y).col
Next y
Next x
Graphic Clear
For x = 50 To 149 : For y = 50 To 149
Graphic Set Pixel (D(x,y).x,D(x,y).y), D(x,y).Col
Next y : Next x
Graphic Redraw
End Sub
Sub RotateImage_GetPixelC
Local w,h,x,y,XCenter,YCenter As Long, sintheta, costheta As Single, bmp$
Dim D(200,200) As gbPoint
XCenter = 100 : YCenter = 100
sintheta = sin(theta) : costheta = cos(theta)
Graphic Attach hDlg, %IDC_Graphic, Redraw
Graphic Render "cowgirl", (50,50)-(149,149)
Graphic Get Bits To bmp$
w = CVL(bmp$,1) : h = CVL(bmp$,5)
For x = 50 to 149
For y = 50 to 149
D(x,y).x = XCenter + (x - XCenter) * Costheta - (y - YCenter) * Sintheta
D(x,y).y = YCenter + (x - XCenter) * Sintheta + (y - YCenter) * Costheta
D(x,y).Col = CVL(bmp$, (y*w+x)*4+8 )
Next y
Next x
Graphic Clear : Graphic Get Bits To bmp$
For x = 50 to 149
For y = 50 to 149
Mid$(bmp$,(D(x,y).y * w + D(x,y).x)*4+8,4) = Mkl$(D(x,y).col)
Next y
Next x
Graphic Set Bits bmp$ : Graphic Redraw
End Sub
'gbs_00709
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm