[Crossplatform] Drawing square roots
Posted: Thu Jul 30, 2009 9:33 pm
This procedure draws text within a square root symbol to an image.
The font and the image, which will be used, must be loaded before calling the procedure (pass the PureBasic-number as a parameter - FontNumber and ImageNumber respectively).
XPos and YPos sets the horizontal and vertical position of the text.
The three string-parameters contain the text, which will be drawn (the middle one is within the square root symbol).
By setting ClearImage to a non-zero value, the given image will be cleared (using FillArea() - thus it should only be used at the first call to an image).
The font and the image, which will be used, must be loaded before calling the procedure (pass the PureBasic-number as a parameter - FontNumber and ImageNumber respectively).
XPos and YPos sets the horizontal and vertical position of the text.
The three string-parameters contain the text, which will be drawn (the middle one is within the square root symbol).
By setting ClearImage to a non-zero value, the given image will be cleared (using FillArea() - thus it should only be used at the first call to an image).
Code: Select all
Procedure CreateSquareRootImage(FontNumber, ImageNumber, XPos, YPos, TextBefore$ = "", RootText$ = "", TextAfter$ = "", ClearImage = 0)
Protected Width, Height, NewYPos
If Not IsFont(FontNumber) Or Not IsImage(ImageNumber) : ProcedureReturn 0 : EndIf
StartDrawing(ImageOutput(ImageNumber))
If ClearImage : FillArea(0, 0, -1, #White) : EndIf
DrawingFont(FontID(FontNumber))
DrawingMode(#PB_2DDrawing_Transparent)
Width = TextWidth(RootText$) + 2
Height = TextHeight(TextBefore$ + RootText$ + TextAfter$)
NewYPos = YPos + Height - 2
If TextBefore$ : XPos = DrawText(XPos, YPos + 1, TextBefore$, #Black) + 2 : EndIf
If RootText$
Line(XPos, YPos + (Height / 2), 3, -2, #Black)
LineXY(XPos + 2, YPos + (Height / 2) - 2, XPos + 5, NewYPos, #Black)
LineXY(XPos + 3, YPos + (Height / 2) - 1, XPos + 5, NewYPos, #Black)
LineXY(XPos + 5, NewYPos, XPos + 8, YPos, #Black)
LineXY(XPos + 5, NewYPos + 1, XPos + 9, YPos, #Black)
Line(XPos + 8, YPos, Width + 3, 0)
DrawText(XPos + 12, YPos + 1, RootText$, #Black)
EndIf
If TextAfter$ : DrawText(XPos + 12 + Width, YPos + 1, TextAfter$, #Black) : EndIf
StopDrawing()
ProcedureReturn NewYPos + 1
EndProcedure
CreateImage(0, 280, 100)
LoadFont(0, "Arial", 11)
y = CreateSquareRootImage(0, 0, 5, 5, "(", "2", ")" + Chr(178) + " = 2", 1)
y = CreateSquareRootImage(0, 0, 15, y + 8, "c =", "a" + Chr(178) + " + " + "b" + Chr(178), "")
CreateSquareRootImage(0, 0, 25, y + 8, "No square root")
OpenWindow(0, 100, 100, 300, 300, "")
ImageGadget(0, 0, 0, 280, 100, ImageID(0))
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
End