• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[Snippet] LineSegment

Level 7
Joined
Apr 27, 2011
Messages
272
I have seen a lot of maps that individually attach coordinate datas for line segments (which would add a little more process for retrieval) while it can be just capsulated by a single data (pointer). The snippet is simple but makes things more easy.

JASS:
library LineSegment
//===========================================================================
//    "LineSegment" by Alain.Mark (Please give credits when used)
//
// -Info-
//    -A simple code for creating, destroying, moving, and measuring(the length of) line
//    segments.
//
// -API-
//    *Creating and Destroying
//    -function CreateSegment takes real x1, real y1, real x2, real y2 returns integer
//    -function DestroySegment takes integer segmentID returns nothing
//    -function PolarlyCreateSegment takes real ox, real oy, real length, real angle returns nothing
//
//    *Utility Functions
//     -function MoveSegment takes integer segmentID, real nx1, real ny1, real nx2, real ny2 returns nothing
//     -function GetSegmentQuasiLength takes segmentID returns real
//     -function GetSegmentLength takes segmentID returns real
//     -function GetSegmentX1 takes integer segmentID returns real
//     -function GetSegmentY1 takes integer segmentID returns real
//     -function GetSegmentX2 takes integer segmentID returns real
//     -function GetSegmentY2 takes integer segmentID returns real
//
//===========================================================================
    globals
        // globals for coordinate storage
        private real array x1Q
        private real array y1Q
        private real array x2Q
        private real array y2Q
        private real array qrQ
        private real array trQ
    endglobals
    globals
        // globals for segement allocation
        private integer counter =-1
        private integer recycle = 0
        private integer array recbin
    endglobals
    
//===========================================================================
    function CreateSegment takes real x1, real y1, real x2, real y2 returns integer
        local integer segmentID
        if(recycle==0)then
            set counter=counter+1
            set segmentID=counter
        else
            set recycle=recycle-1
            set segmentID=recbin[recycle]
        endif
        set x1Q[segmentID]=x1
        set y1Q[segmentID]=y1
        set x2Q[segmentID]=x2
        set y2Q[segmentID]=y2
        set qrQ[segmentID]=(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)
        set trQ[segmentID]=SquareRoot(qrQ[segmentID])
        return segmentID
    endfunction
    
//===========================================================================
    function PolarlyCreateSegment takes real ox, real oy, real length, real a returns integer
        return CreateSegment(ox,oy,ox+length*Cos(a*0.01745),oy+Sin(a*0.01745))
    endfunction
    
//===========================================================================
    function UpdateSegment takes integer segmentID, real nx1, real ny1, real nx2, real ny2 returns nothing
        set x1Q[segmentID]=nx1
        set y1Q[segmentID]=ny1
        set x2Q[segmentID]=nx2
        set y2Q[segmentID]=ny2
        set qrQ[segmentID]=(nx2-nx1)*(nx2-nx1)+(ny2-ny1)*(ny2-ny1)
        set trQ[segmentID]=SquareRoot(qrQ[segmentID])        
    endfunction
    function MoveSegment takes integer segmentID, real nx1, real ny1, real nx2, real ny2 returns nothing
        call UpdateSegment(segmentID,nx1,ny1,nx2,ny2)    
    endfunction
    
//===========================================================================
    function DestroySegment takes integer segmentID returns nothing
        set recbin[recycle]=segmentID
        set recycle=recycle+1
    endfunction
    
//===========================================================================
    constant function GetSegmentX1 takes integer segmentID returns real
        return x1Q[segmentID]
    endfunction
    
//===========================================================================
    constant function GetSegmentY1 takes integer segmentID returns real
        return y1Q[segmentID]
    endfunction
    
//===========================================================================
    constant function GetSegmentX2 takes integer segmentID returns real
        return x2Q[segmentID]
    endfunction
    
//===========================================================================
    constant function GetSegmentY2 takes integer segmentID returns real
        return y2Q[segmentID]
    endfunction
    
//===========================================================================
    constant function GetSegmentLength takes integer segmentID returns real
        return trQ[segmentID]
    endfunction
    
//===========================================================================
    constant function GetSegmentQuasiLength takes integer segmentID returns real
        return qrQ[segmentID]
    endfunction
    
//===========================================================================
endlibrary
 
Last edited:
Level 6
Joined
Jun 20, 2011
Messages
249
For better allocation delete the recycler global and do this
JASS:
set n = recbin[0]
set recbin[0] = recbin[n]
Also, you should read my Loc snippet, combined with AdvLoc pretty much does this, but keeps essential data such as angle, distance and slope between the two points
 
Top