I tried a bit more..
Now I store the coords first, get the actual bounding box, shift them, scale them, shift them back, so they fit the given box. Second row on screen is scaled, first row is unscaled.
Code: Select all
EnableExplicit
Define ww, wh, style, win, canvas, event, quit
ww=1000
wh=600
style | #PB_Window_ScreenCentered
style | #PB_Window_SystemMenu
style | #PB_Window_MinimizeGadget
win = OpenWindow(#PB_Any, 50,100, ww,wh, "use mousemove / mousewheel", style)
AddKeyboardShortcut(win, #PB_Shortcut_Escape, 10)
canvas = CanvasGadget(#PB_Any, 0, 0, ww, wh, #PB_Canvas_Keyboard)
SetActiveGadget(canvas)
Define w, h
Define angle.f
Procedure draw(x.f, y.f, w.f, h.f, n, a.f, scaleTofit = #False)
Protected.f xr, yr
Protected.f xc, yc
Protected.f xp, yp
Protected Dim p.f(n, 1) ; (0 = x, 1 = y)
Protected.f xmin = 9999999
Protected.f xmax = -9999999
Protected.f ymin = 9999999
Protected.f ymax = -9999999
Protected.f wPoly, hPoly
Protected.f wr, hr
Protected.f x1, y1, x2, y2
Protected i
Protected.f a2
DrawingMode(#PB_2DDrawing_AlphaBlend | #PB_2DDrawing_Outlined)
; draw given bounding box (red)
Box(x, y, w, h, $aa0000ff)
; radius
xr = (w / 2)
yr = (h / 2)
; center
xc = x + xr
yc = y + yr
; get points and actual polygon boundary box (min/max)
For i=0 To n
a2.f = Radian(i * 360.0 / n) + Radian(a)
xp = xc + xr * Cos(a2)
yp = yc + yr * Sin(a2)
p(i, 0) = xp
p(i, 1) = yp
If xp < xmin : xmin = xp : EndIf
If xp > xmax : xmax = xp : EndIf
If yp < ymin : ymin = yp : EndIf
If yp > ymax : ymax = yp : EndIf
Next
If scaleTofit = #False
; draw unscaled polygon (purple)
For i=1 To n
x1 = p(i-1, 0)
y1 = p(i-1, 1)
x2 = p(i , 0)
y2 = p(i , 1)
LineXY(x1, y1, x2, y2, $aaffbbbb)
Next
; draw bounding box/circle (purple)
;Ellipse(xc, yc, xr, yr, $aaffbbbb)
;Box( (xmin), (ymin), (xmax - xmin), (ymax - ymin), $aaffbbbb)
Else
; shift points to zero
For i=0 To n
p(i, 0) - xmin
p(i, 1) - ymin
Next
; actual size needed for polygon
wPoly = (xmax - xmin) + 1
hPoly = (ymax - ymin) + 1
; get ratio from given box to needed box
wr = w / wPoly
hr = h / hPoly
; scale points
For i=0 To n
p(i, 0) * wr
p(i, 1) * hr
Next
; shift points back to given origin
For i=0 To n
p(i, 0) + x
p(i, 1) + y
Next
; draw scaled polygon (green)
For i=1 To n
x1 = p(i-1, 0)
y1 = p(i-1, 1)
x2 = p(i , 0)
y2 = p(i , 1)
LineXY(x1, y1, x2, y2, $ff66ff66)
Next
EndIf
EndProcedure
Procedure redraw()
Shared w, h, angle, canvas
StartDrawing(CanvasOutput(canvas))
DrawingMode(#PB_2DDrawing_AllChannels)
Box(0, 0, OutputWidth(), OutputHeight(), $00000000)
draw( 0, 0, w, h, 3, angle)
draw(200, 0, w, h, 4, angle)
draw(400, 0, w, h, 5, angle)
draw(600, 0, w, h, 6, angle)
draw(800, 0, w, h, 12, angle)
draw( 0, 200, w, h, 3, angle, #True)
draw(200, 200, w, h, 4, angle, #True)
draw(400, 200, w, h, 5, angle, #True)
draw(600, 200, w, h, 6, angle, #True)
draw(800, 200, w, h, 12, angle, #True)
StopDrawing()
EndProcedure
Repeat
If IsWindow(win) ;{
Repeat
event = WaitWindowEvent(10)
Select event
Case #PB_Event_CloseWindow
quit = #True
Case #PB_Event_Menu
Select EventMenu()
Case 10
quit = #True
EndSelect
Case #PB_Event_Gadget
Select EventGadget()
Case canvas
Select EventType()
Case #PB_EventType_MouseWheel
angle + 4 * GetGadgetAttribute(canvas, #PB_Canvas_WheelDelta)
redraw()
Case #PB_EventType_MouseMove
w = GetGadgetAttribute(canvas, #PB_Canvas_MouseX)
h = GetGadgetAttribute(canvas, #PB_Canvas_MouseY)
redraw()
EndSelect
EndSelect
EndSelect
Until Not event
;}
EndIf
Until quit