Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inside-Outside a Triangle
#1
This is an old program from .org and is archived here as an illustrative example of an InForm program and is updated to run in PE.  It was written as a response to a challenge to determine whether the cursor is inside or outside of any given triangle.

   

The InForm objects used are:

PictureBox
Label
Button
Frame

Unzip the file and extract the folder into your PEQB64 directory.  In the IDE make sure that you have the Run Option “Save EXE in source folder” checked.

.zip   Triangle.zip (Size: 129.33 KB / Downloads: 4)


In the running program, you first have to set the three vertices of the triangle.  Then a label shows if the cursor is inside or outside.  Passing from one to the other is signalled by a sound.

It is a simple program but InForm gives it a pleasing look.
Code: (Select All)
': Find out if Cursor is Inside or Outside of a Drawn Triangle, by Magdha 2025-12-26 ex Qwerkey
': After problem set in .org by STxAxTIC = Sprezzo

': Project by
': This program uses
': InForm-PE for QB64-PE - v1.5.8 based upon InForm by Fellippe Heitor
': Copyright (c) 2025 QB64 Phoenix Edition Team
': https://github.com/QB64-Phoenix-Edition/InForm-PE
'-----------------------------------------------------------


': Controls' IDs: ------------------------------------------------------------------
DIM SHARED InsideOutsideTriangle AS LONG
DIM SHARED PictureBox1 AS LONG
DIM SHARED XLB AS LONG
DIM SHARED YLB AS LONG
DIM SHARED Pos1LB AS LONG
DIM SHARED Pos2LB AS LONG
DIM SHARED Pos3LB AS LONG
DIM SHARED X1LB AS LONG
DIM SHARED Y1LB AS LONG
DIM SHARED X2LB AS LONG
DIM SHARED Y2LB AS LONG
DIM SHARED X3LB AS LONG
DIM SHARED Y3LB AS LONG
DIM SHARED ExitBT AS LONG
DIM SHARED RestartBT AS LONG
DIM SHARED InsideOutsideLB AS LONG
DIM SHARED ClickFrame AS LONG
DIM SHARED Click1LB AS LONG
DIM SHARED Click2LB AS LONG
DIM SHARED PictureBox2 AS LONG
DIM SHARED CXLB AS LONG
DIM SHARED CYLB AS LONG

CONST SmallErr# = 1E-11 'Need SmallErr to check sum is close to zero (would be exactly zero if no calculation errors)
DIM SHARED ClickCount%%, Vertices%(2, 1), A#, B#, C#, XMouse%, YMouse%, IsInside%%, SoftBeep&

': External modules: ---------------------------------------------------------------
'$INCLUDE:'InForm\InForm.bi'
'$INCLUDE:'InForm\xp.uitheme'
'$INCLUDE:'Inside-Outside Triangle.frm'

': Functions: ---------------------------------------------------------------------
FUNCTION SideLength# (X1#, Y1#, X2#, Y2#)
    'Calculates Side Length from Vertices
    SideLength# = SQR((X1# - X2#) * (X1# - X2#) + (Y1# - Y2#) * (Y1# - Y2#))
END FUNCTION

FUNCTION Angle# (L1#, L2#, L3#)
    'Derives vertex angle from 3 sides and rule of cosines
    Angle# = _ACOS(((L2# * L2#) + (L3# * L3#) - (L1# * L1#)) / (2 * L2# * L3#))
END FUNCTION

FUNCTION AtCorner%%
    'This function avoids trig divide by zero difficulty at triangle corners
    IF (XMouse% = Vertices%(0, 0) AND YMouse% = Vertices%(0, 1)) OR (XMouse% = Vertices%(1, 0) AND YMouse% = Vertices%(1, 1)) OR (XMouse% = Vertices%(2, 0) AND YMouse% = Vertices%(2, 1)) THEN
        AtCorner%% = True
    ELSE
        AtCorner%% = False
    END IF
END FUNCTION

FUNCTION InPicture%%
    'True if cursor is in PictureBox1 (inset by 1)
    IF XMouse% > 0 AND XMouse% < 649 AND YMouse% > 0 AND YMouse% < 539 THEN
        InPicture%% = True
    ELSE
        InPicture%% = False
    END IF
END FUNCTION

FUNCTION TrimIt$ (T%)
    TrimIt$ = LTRIM$(STR$(T%))
END FUNCTION

': Event procedures & Process Subs: -----------------------------------------------
SUB __UI_BeforeInit
    A# = 1
    B# = 1
    C# = 1
    FOR N%% = 0 TO 2
        FOR M%% = 0 TO 1
            Vertices%(N%%, M%%) = 1
        NEXT M%%
    NEXT N%%
    IsInside%% = False
    SoftBeep& = _SNDOPEN("WeeBeep.mp3")
END SUB

SUB __UI_OnLoad
    _SCREENMOVE 100, 50
    Control(InsideOutsideLB).Hidden = True
    Control(PictureBox1).Stretch = False
    BeginDraw PictureBox1
    'Drawing code goes here
    COLOR _RGB32(0, 0, 0), _RGB32(255, 255, 255)
    CLS
    EndDraw PictureBox1
    BeginDraw PictureBox2
    _PUTIMAGE , _LOADIMAGE("cursor.png", 32)
    EndDraw PictureBox2
END SUB

SUB __UI_BeforeUpdateDisplay
    'This event occurs at approximately 30 frames per second.
    'You can change the update frequency by calling SetFrameRate DesiredRate%
    XMouse% = __UI_MouseLeft - 10
    YMouse% = __UI_MouseTop - 10
    IF InPicture%% THEN
        IF NOT AtCorner%% THEN
            'Calculate side lengths of triangle formed with cursor position and triangle vertices
            D# = SideLength#(XMouse%, YMouse%, Vertices%(1, 0), Vertices%(1, 1))
            E# = SideLength#(XMouse%, YMouse%, Vertices%(2, 0), Vertices%(2, 1))
            F# = SideLength#(XMouse%, YMouse%, Vertices%(0, 0), Vertices%(0, 1))
            AlphaDash# = Angle#(A#, E#, F#)
            BetaDash# = Angle#(B#, D#, F#)
            GammaDash# = Angle#(C#, D#, E#)
            IF ABS(AlphaDash# + BetaDash# + GammaDash# - _PI(2)) < SmallErr# THEN
                Caption(InsideOutsideLB) = "INSIDE"
                IF NOT IsInside%% THEN
                    _SNDPLAY SoftBeep&
                    IsInside%% = True
                END IF
            ELSE
                Caption(InsideOutsideLB) = "OUTSIDE"
                IF IsInside%% THEN
                    _SNDPLAY SoftBeep&
                    IsInside%% = False
                END IF
            END IF
        END IF
        Caption(CXLB) = TrimIt$(XMouse%)
        Caption(CYLB) = TrimIt$(YMouse%)
    ELSE
        Caption(CXLB) = ""
        Caption(CYLB) = ""
    END IF
END SUB

SUB __UI_BeforeUnload
END SUB

SUB __UI_Click (id AS LONG)
    SELECT CASE id
        CASE PictureBox1
            'Define Positions of Triangle Vertices
            IF InPicture%% THEN
                IF ClickCount%% <= 2 THEN
                    Vertices%(ClickCount%%, 0) = XMouse%
                    Vertices%(ClickCount%%, 1) = YMouse%
                    SELECT CASE ClickCount%%
                        CASE 0
                            Caption(X1LB) = TrimIt$(XMouse%)
                            Caption(Y1LB) = TrimIt$(YMouse%)
                            BeginDraw PictureBox1
                            CALL DrawTriangle
                            EndDraw PictureBox1
                            ClickCount%% = ClickCount%% + 1
                            Caption(Click2LB) = "Vertex 2"
                        CASE 1
                            Caption(X2LB) = TrimIt$(XMouse%)
                            Caption(Y2LB) = TrimIt$(YMouse%)
                            BeginDraw PictureBox1
                            CALL DrawTriangle
                            EndDraw PictureBox1
                            ClickCount%% = ClickCount%% + 1
                            Caption(Click2LB) = "Vertex 3"
                        CASE 2
                            Caption(X3LB) = TrimIt$(XMouse%)
                            Caption(Y3LB) = TrimIt$(YMouse%)
                            BeginDraw PictureBox1
                            CALL DrawTriangle
                            EndDraw PictureBox1
                            ClickCount%% = ClickCount%% + 1
                            Caption(Click1LB) = "Move Cursor"
                            Caption(Click2LB) = "Around"
                            Control(InsideOutsideLB).Hidden = False
                            'Triangle Side Lengths
                            A# = SideLength#(Vertices%(0, 0), Vertices%(0, 1), Vertices%(2, 0), Vertices%(2, 1))
                            B# = SideLength#(Vertices%(1, 0), Vertices%(1, 1), Vertices%(0, 0), Vertices%(0, 1))
                            C# = SideLength#(Vertices%(2, 0), Vertices%(2, 1), Vertices%(1, 0), Vertices%(1, 1))
                    END SELECT
                END IF
            END IF
        CASE ExitBT
            SYSTEM
        CASE RestartBT
            ClickCount%% = 0
            BeginDraw PictureBox1
            CLS
            EndDraw PictureBox1
            Caption(X1LB) = ""
            Caption(Y1LB) = ""
            Caption(X2LB) = ""
            Caption(Y2LB) = ""
            Caption(X3LB) = ""
            Caption(Y3LB) = ""
            Control(InsideOutsideLB).Hidden = True
            Caption(Click1LB) = "Click To Set"
            Caption(Click2LB) = "Vertex 1"
    END SELECT
END SUB

SUB __UI_MouseEnter (id AS LONG)
END SUB

SUB __UI_MouseLeave (id AS LONG)
END SUB

SUB __UI_FocusIn (id AS LONG)
END SUB

SUB __UI_FocusOut (id AS LONG)
END SUB

SUB __UI_MouseDown (id AS LONG)
END SUB

SUB __UI_MouseUp (id AS LONG)
END SUB

SUB __UI_KeyPress (id AS LONG)
END SUB

SUB __UI_TextChanged (id AS LONG)
END SUB

SUB __UI_ValueChanged (id AS LONG)
END SUB

SUB __UI_FormResized
END SUB

SUB DrawTriangle
    LINE (Vertices%(ClickCount%%, 0) - 2, Vertices%(ClickCount%%, 1))-(Vertices%(ClickCount%%, 0) + 2, Vertices%(ClickCount%%, 1))
    LINE (Vertices%(ClickCount%%, 0), Vertices%(ClickCount%%, 1) - 2)-(Vertices%(ClickCount%%, 0), Vertices%(ClickCount%%, 1) + 2)
    IF ClickCount%% >= 1 THEN
        LINE (Vertices%(ClickCount%% - 1, 0), Vertices%(ClickCount%% - 1, 1))-(Vertices%(ClickCount%%, 0), Vertices%(ClickCount%%, 1))
        IF ClickCount%% = 2 THEN LINE (Vertices%(2, 0), Vertices%(2, 1))-(Vertices%(0, 0), Vertices%(0, 1))
    END IF
END SUB

'$INCLUDE:'InForm\InForm.ui'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pascal's Triangle Pinball Demo Magdha 0 222 11-28-2025, 11:58 AM
Last Post: Magdha

Forum Jump:


Users browsing this thread: 1 Guest(s)