Jump to content

Plot coordinate pairs: Difference between revisions

→‎{{header|PureBasic}}: Corrected Y Scale, refined code
(→‎{{header|PureBasic}}: Corrected Y Scale, refined code)
Line 445:
EndStructure
 
Global i, x, y.f, max_x, max_y, min_x = #MAXLONG, min_y = Infinity()
GlobalDefine count = (?serie_y - ?serie_x) / SizeOf(Integer) - 1
Global Dim MyData.PlotData(count)
 
Restore serie_x
For i = 0 To count
Read.i x
MyData(i)\x = x
If x > max_x: max_x = x: EndIf
If x < min_x: min_x = x: EndIf
Next
Restore serie_y
For i = 0 To count
Read.f y
MyData(i)\y = y
If y > max_y: max_y = y: EndIf
If y < min_y: min_y = y: EndIf
Next
 
Procedure UpdatePlot(Win, w, h)
Static gblm = 20, gtrm = 5 ;graph's bottom-left and top-right margin
If w>20 And h>20
SetWindowTitle(win,"PureBasic Plot "+Str(w)+"x"+Str(h))
Protected x, y, i, yf.f,count xf.f,= imgArraySize(MyData())
If w > gblm And h > gblm And count > 0
Protected x0=20, y0=h-20, amounts=ArraySize(MyData())
SetWindowTitle(winWin, "PureBasic Plot " + Str(w) + "x" + Str(h))
Protected x1,x2,y1,y2
Protected gw = w - gblm, gh = h - gblm ;graph's width and height
yf=(h-20)/max_y*0.99
Protected x1i,x2 yf.f,y1,y2 xf.f
xf=(w-20)/max_x*0.99
yf = (hgh -20 gtrm) / max_y*0.99
CreateImage(0,w,h)
xf = (wgw -20 gtrm) / max_x*0.99
Define OutputID=ImageOutput(0)
Next
CreateImage(0, w, h)
DefineProtected OutputID = ImageOutput(0)
StartDrawing(OutputID)
DrawingMode(#PB_2DDrawing_Transparent)
LineXY(20,h-20,w,h-20,$FFFFFF)
LineXY(20,h-20,20,0,$FFFFFF)
For i=0 To amounts
;- Draw grid
x1=20:For i x2=w 0 To count
y1 y =(h-20) gh - max_y * i /amounts count * yf
LineXY(x1gblm,y1 y,x2 w - gtrm,y2 y, $467E3E)
y2=y1
; Y-scale
LineXY(x1,y1,x2,y2,$467E3E)
DrawText(1,y1 y - 5, RSet(StrD(i /10 count * max_y, 1), 5))
; Y-scale
x = gblm + max_x * i / count * xf
DrawText(1,y1-5,RSet(StrD(i/10*max_y,1),5))
x1 y =20+max_x*i/amounts*xf gh
x2=x1: y1=(h; X-20): y2=0Scale
LineXY(20x,h-20 y,20 x,0 gtrm, $FFFFFF467E3E)
; X-Scale
If i: DrawText(x1x - 5,y1 y + 2, Str(i)): EndIf
LineXY(x1,y1,x2,y2,$467E3E)
y2=y1Next
If i: DrawText(x1-5,y1+2,Str(i)): EndIf
;- Draw curve
Protected ox = gblm, oy = gh, x, y
x=20+MyData(i)\x*xf
For i = 0 To amountscount
y=(h-20)-MyData(i)\y*yf
x =20 gblm + MyData(i)\x * xf
LineXY(x0,y0,x,y,$0133EE)
x0=x: y0 y = gh - MyData(i)\y * yf
LineXY(20ox,h-20 oy,w x,h-20 y, $FFFFFF0133EE)
Next
ox = x: oy = y
Next
StopDrawing()
ImageGadget(0, 0, 0, w, h, ImageID(0))
EndIf
EndProcedure
 
Define FlagsWin = OpenWindow(#PB_Any, 0, 0, 600, 400,"", #PB_Window_SystemMenu | #PB_Window_SizeGadget)
Define Win=OpenWindow(#PB_Any,0,0,600,400,"",Flags)
If Win
SmartWindowRefresh(Win, 1)
UpdatePlot(Win, WindowWidth(Win), WindowHeight(winWin))
Repeat
Define Eventevent = WaitWindowEvent()
Select Eventevent
Case #PB_Event_SizeWindow
UpdatePlot(Win, WindowWidth(Win), WindowHeight(winWin))
EndSelect
Until Eventevent = #PB_Event_CloseWindow
; Save the plot if the user wants to
Flags=If MessageRequester("Question", "Save it?", #PB_MessageRequester_YesNo) = #PB_MessageRequester_Yes
Define fileFile$=SaveFileRequester("Save as", "PB.png", "PNG (*.png)|*.png", 0)
If Flags=#PB_MessageRequester_Yes
Define file$=SaveFileRequester("Save as","PB.png","PNG (*.png)|*.png",0)
UsePNGImageEncoder()
SaveImage(0,file File$, #PB_ImagePlugin_PNG)
EndIf
EndIf
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.