Discussion Most efficient way to create various shapes

Executor

I see you
Reaction score
57
I'm currently working on a system which shall allow the easy and efficient creation of shapes. Therefore I'd like to discuss what's the best way.

Line

JASS:
function CreateLine takes real x, real y, real angle, real offset .... returns nothing
    local real vx         = Cos(angle) * offset
    local real vy         = Sin(angle) * offset
    loop
        set x = x + vx
        set y = y + vy 
        // do stuff
    endloop
endfunction


I think this is the best way


Circle


JASS:
function CreateCircle takes real mx, real my, real r, real angle, ... returns nothing
    local integer step = 360
    local real x = 0
    local real y = 0
    
    loop
        set angle = angle + 1
        set step = step - 1
        set x = mx + Cos(angle) * r
        set y = my + Sin(angle) * r
        // do stuff
    endloop
endfunction


I think there should be a better possibility than converting the angle again and again via cos and sin



Which further shapes shall I include?
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
Hmm, the most efficient way is to rearrange the equation of shape and put them into a function.
 

Executor

I see you
Reaction score
57
JASS:
library ShapeCreator

    struct ShapeData
        real x
        real y       
        integer customValue
        
        private integer totalRest
        private integer stepRest
        private integer tempInt1
        private real    tempReal1
        private real    tempReal2
        private real    tempReal3
        private real    tempReal4
        private real    tempReal5
        
        private timer intervalTimer
        private timer codeExecuter      
        
        private static constant real PI2 = 2*bj_PI
        
        private static hashtable link
        private static method onInit takes nothing returns nothing
            set .link = InitHashtable()
        endmethod
        
        private static method onPause takes nothing returns nothing
            local thistype this = LoadInteger(.link,GetHandleId(GetExpiredTimer()),0)
            call PauseTimer(.codeExecuter)
            call .destroy()
        endmethod
        
        private static method onCreateLineTimed takes nothing returns nothing
            local thistype this = LoadInteger(.link,GetHandleId(GetExpiredTimer()),0)
            set .x              = .x + .tempReal1
            set .y              = .y + .tempReal2
            set .totalRest      = .totalRest - 1
            if .totalRest < 1 then
                call PauseTimer(.intervalTimer)
                call TimerStart(.intervalTimer,0.,false,function thistype.onPause)
            endif
        endmethod
                
        static method createLineTimed takes real sx, real sy, real angle, real offset, real distance, real interval, code c returns thistype
            local thistype this = thistype.allocate()
            
            set .tempReal1  = Cos(angle) * offset
            set .tempReal2  = Sin(angle) * offset
            set .x          = sx - .tempReal1
            set .y          = sy - .tempReal2
            set .totalRest  = R2I(distance/offset-0.5)
            
            if .intervalTimer == null then
                set .intervalTimer = CreateTimer()
                set .codeExecuter  = CreateTimer()
            endif
            call SaveInteger(.link,GetHandleId(.intervalTimer),0,this)
            call SaveInteger(.link,GetHandleId(.codeExecuter),0,this)
            call TimerStart(.intervalTimer,interval,true,function thistype.onCreateLineTimed)
            call TimerStart(.codeExecuter,interval,true,c)
            return this
        endmethod
        
        private static method onCreateCircleTimed takes nothing returns nothing
            local thistype this = LoadInteger(.link,GetHandleId(GetExpiredTimer()),0)
            set .x              = .tempReal1 + Cos(.tempReal3) * .tempReal4
            set .y              = .tempReal2 + Sin(.tempReal3) * .tempReal4
            set .tempReal3      = .tempReal3 + .tempReal5 * .tempInt1
            if .tempReal3 > .PI2 then
                set .tempReal3 = .tempReal3 - .PI2
            endif
            set .totalRest      = .totalRest - 1
            if .totalRest < 1 then
                call PauseTimer(.intervalTimer)
                call TimerStart(.intervalTimer,0.,false,function thistype.onPause)
            endif
        endmethod
        
        static method createCircleTimed takes real mx, real my, real r, real startangle, integer direction, integer count, real interval, code c returns thistype
            local thistype this = thistype.allocate()
            
            set .totalRest  = count
            set .tempReal1  = mx
            set .tempReal2  = my
            set .tempReal3  = startangle
            set .tempReal4  = r
            set .tempReal5  = .PI2/count * direction
            
            if .intervalTimer == null then
                set .intervalTimer = CreateTimer()
                set .codeExecuter  = CreateTimer()
            endif
            call SaveInteger(.link,GetHandleId(.intervalTimer),0,this)
            call SaveInteger(.link,GetHandleId(.codeExecuter),0,this)
            call TimerStart(.intervalTimer,interval,true,function thistype.onCreateCircleTimed)
            call TimerStart(.codeExecuter,interval,true,c)
            return this
        endmethod
        
        private static method onCreateSquareTimed takes nothing returns nothing
            local thistype this = LoadInteger(.link,GetHandleId(GetExpiredTimer()),0)
            if .tempInt1 == 0 then
                set .stepRest = .stepRest - 1
                set .tempInt1 = .stepRest
                return
            endif
            set .x              = .x + .tempReal4                   // x + vx
            set .y              = .y + .tempReal5                   // y + vy
            
            set .stepRest   = .stepRest - 1
            if .stepRest < 1 then
                set .tempReal1 = .tempReal1 + .PI2*0.25*.tempReal3  // next edge
                if .tempReal1 > .PI2 then
                    set .tempReal1 = .tempReal1 - .PI2
                endif
                set .tempReal4 = Cos(.tempReal1) * tempReal2        // vx
                set .tempReal5 = Sin(.tempReal1) * tempReal2        // vy
                set .stepRest = .tempInt1
            endif
            
            set .totalRest  = .totalRest - 1
            if .totalRest < 1 then
                call PauseTimer(.intervalTimer)
                call TimerStart(.intervalTimer,0.,false,function thistype.onPause)
            endif
        endmethod
        
        static method createSquareTimed takes real mx, real my, real angleToStartCorner, real edgeLength, integer dir, integer countPerEdge, real interval, code c returns thistype
            local thistype this = thistype.allocate()
            
            set .tempReal2  =  edgeLength / countPerEdge                            // step
            
            set edgeLength = (edgeLength / 2) * SquareRoot(2)
            
            set .x = mx + Cos(angleToStartCorner) * edgeLength
            set .y = my + Sin(angleToStartCorner) * edgeLength
            set .tempInt1   = 0            
            set .tempReal1  = ModuloReal(angleToStartCorner + .PI2*-0.625*dir,.PI2) // angle               
            set .tempReal3  = dir                                                   // direction
            set .tempReal4  = Cos(.tempReal1) * tempReal2                           // vx
            set .tempReal5  = Sin(.tempReal1) * tempReal2                           // vy
            set .totalRest  = countPerEdge * 4 - 5
            set .stepRest   = countPerEdge
            
            if .intervalTimer == null then
                set .intervalTimer = CreateTimer()
                set .codeExecuter  = CreateTimer()
            endif
            call SaveInteger(.link,GetHandleId(.intervalTimer),0,this)
            call SaveInteger(.link,GetHandleId(.codeExecuter),0,this)
            call TimerStart(.intervalTimer,interval,true,function thistype.onCreateSquareTimed)
            call TimerStart(.codeExecuter,interval,true,c)
            return this
            return this
        endmethod
        
        static method getData takes nothing returns thistype
            return LoadInteger(.link,GetHandleId(GetExpiredTimer()),0)
        endmethod
    endstruct

endlibrary


thats my current code, I'm still working on it.
Don't kill me because of the "tempReal1" .. as I construct various shapes with the same struct thats necessary. I can maybe later on replace the names.
 

PurgeandFire

zxcvmkgdfg
Reaction score
509
Just be sure that you don't add too many fluff functions. Most people who'd use this system would probably have the common sense enough to use createLine timed to suit most line-oriented shapes.

Try to do things like hypocycloids, roses, epicycloids, and all those. Those are the more unique and awesome ones. =D

Daelin has a sweet system - Graphic System - to create these, but they no longer work due to the fact that it used handle vars and the return bug. This featured some awesome shapes, in case you'd like to use it as a reference for some equations.

In fact, I was thinking of making a newer graphic system as well. But who knows when I'd ever get done. =P
 

Executor

I see you
Reaction score
57
Ye, I recognized this system after I began, but since it is broken (and is unefficient)....

Atm I'm simply having problems with the mathemtical relations. Here is the current code, it already works and you may test and see whats my problem.
(The figures are created, but with an offset to the midpoint)

JASS:
library ShapeCreator

    struct ShapeData
        real x
        real y       
        integer customValue
        
        private integer totalRest
        private integer restOnStage
        private integer countPerStage
        private real    modAngle
        private real    currAngle
        private real    vx
        private real    vy
        private real    offset
        
        private timer intervalTimer
        private timer codeExecuter      
        
        private static constant real PI2 = 2*bj_PI
        
        private static hashtable link
        private static method onInit takes nothing returns nothing
            set .link = InitHashtable()
        endmethod
        
        private static method onPause takes nothing returns nothing
            local thistype this = LoadInteger(.link,GetHandleId(GetExpiredTimer()),0)
            call PauseTimer(.codeExecuter)
            call .destroy()
        endmethod
        
        private static method onCreateLineTimer takes nothing returns nothing
            local thistype this = LoadInteger(.link,GetHandleId(GetExpiredTimer()),0)
            set .x              = .x + .vx
            set .y              = .y + .vy
            set .totalRest      = .totalRest - 1
            if .totalRest < 1 then
                call PauseTimer(.intervalTimer)
                call TimerStart(.intervalTimer,0.,false,function thistype.onPause)
            endif
        endmethod
                
        static method createLine takes real sx, real sy, real angle, real offset, real distance, real interval, code c returns thistype
            local thistype this = thistype.allocate()
            
            set .vx         = Cos(angle) * offset
            set .vy         = Sin(angle) * offset
            set .x          = sx - .vx
            set .y          = sy - .vy
            set .totalRest  = R2I(distance/offset-0.5)
            
            if .intervalTimer == null then
                set .intervalTimer = CreateTimer()
                set .codeExecuter  = CreateTimer()
            endif
            call SaveInteger(.link,GetHandleId(.intervalTimer),0,this)
            call SaveInteger(.link,GetHandleId(.codeExecuter),0,this)
            call TimerStart(.intervalTimer,interval,true,function thistype.onCreateLineTimer)
            call TimerStart(.codeExecuter,interval,true,c)
            return this
        endmethod
        
        private static method onCreateCircleTimer takes nothing returns nothing
            local thistype this = LoadInteger(.link,GetHandleId(GetExpiredTimer()),0)
            set .x              = .vx + Cos(.currAngle) * .offset         // vx => mx; dir => r
            set .y              = .vy + Sin(.currAngle) * .offset         // vx => my; dir => r
            set .currAngle      = ModuloReal(.currAngle + .modAngle,.PI2)
            set .totalRest      = .totalRest - 1
            if .totalRest < 1 then
                call PauseTimer(.intervalTimer)
                call TimerStart(.intervalTimer,0.,false,function thistype.onPause)
            endif
        endmethod
        
        static method createCircle takes real mx, real my, real r, real startangle, integer dir,/*
            */ integer count, real interval, code c returns thistype
            
            local thistype this = thistype.allocate()
            
            set .totalRest  = count
            set .vx         = mx                // vx => mx
            set .vy         = my                // vy => my
            set .currAngle  = startangle
            set .offset     = r                 // dir => r
            set .modAngle   = .PI2/count * dir
            
            if .intervalTimer == null then
                set .intervalTimer = CreateTimer()
                set .codeExecuter  = CreateTimer()
            endif
            call SaveInteger(.link,GetHandleId(.intervalTimer),0,this)
            call SaveInteger(.link,GetHandleId(.codeExecuter),0,this)
            call TimerStart(.intervalTimer,interval,true,function thistype.onCreateCircleTimer)
            call TimerStart(.codeExecuter,interval,true,c)
            return this
        endmethod
        
        private static method onCreateStarPolygonTimer takes nothing returns nothing
            local thistype this = LoadInteger(.link,GetHandleId(GetExpiredTimer()),0)
            if .countPerStage == 0 then
                set .restOnStage    = .restOnStage - 1
                set .countPerStage  = .restOnStage
                return
            endif
            set .x              = .x + .vx       
            set .y              = .y + .vy                
            set .restOnStage    = .restOnStage - 1
            if .restOnStage < 1 then
                set .currAngle      = ModuloReal(.currAngle+.modAngle,.PI2)
                set .vx             = Cos(.currAngle) * .offset    
                set .vy             = Sin(.currAngle) * .offset   
                set .restOnStage    = .countPerStage
            endif
            set .totalRest      = .totalRest - 1
            if .totalRest < 1 then
                call PauseTimer(.intervalTimer)
                call TimerStart(.intervalTimer,0.,false,function thistype.onPause)
            endif
        endmethod
        
        static method createStarPolygon takes real mx, real my, real angleToStartCorner, integer corners,/*
            */real r, integer dir, integer countPerEdge, real interval, code c returns thistype
            local thistype this = thistype.allocate()
            
            local real edgeLength = 0
            
            set .modAngle   = bj_PI/corners
            set edgeLength  = 2*r * Sin(.modAngle)
            
            set .x              = mx + Cos(angleToStartCorner) * r
            set .y              = my + Sin(angleToStartCorner) * r
            set .countPerStage  = 0
            set .currAngle      = ModuloReal(angleToStartCorner+(bj_PI-(.modAngle / 2))*dir,.PI2)
            set .modAngle       = (bj_PI - .modAngle) * dir
            set .offset         = edgeLength/countPerEdge * dir
            set .vx             = Cos(.currAngle) * .offset
            set .vy             = Sin(.currAngle) * .offset
            set .totalRest      = countPerEdge * corners - corners
            set .restOnStage    = countPerEdge
        
            if .intervalTimer == null then
                set .intervalTimer = CreateTimer()
                set .codeExecuter  = CreateTimer()
            endif
            call SaveInteger(.link,GetHandleId(.intervalTimer),0,this)
            call SaveInteger(.link,GetHandleId(.codeExecuter),0,this)
            call TimerStart(.intervalTimer,interval,true,function thistype.onCreateStarPolygonTimer)
            call TimerStart(.codeExecuter,interval,true,c)
            return this
        endmethod
        
        static method getData takes nothing returns thistype
            return LoadInteger(.link,GetHandleId(GetExpiredTimer()),0)
        endmethod
    endstruct

endlibrary

scope Tester initializer InitTest

    private function onTestRepeater takes nothing returns nothing
        local ShapeData sd = ShapeData.getData()
        call DestroyEffect(AddSpecialEffect("Abilities\\Spells\\Human\\SpellSteal\\SpellStealMissile.mdl",sd.x,sd.y))
    endfunction

    private function onTest takes nothing returns nothing
        local unit u = GetTriggerUnit()
        local real x = GetUnitX(u)
        local real y = GetUnitY(u)
        local real a =  Atan2(GetOrderPointY()-y,GetOrderPointX()-x)
        local ShapeData sd = 0
        //set sd = ShapeData.createLine(x,y,a,30.,800.,0.05,function onTestRepeater)
        //set sd = ShapeData.createCircle(x,y,400.,a,1,15,0.,function onTestRepeater)
        //set sd = ShapeData.createSquare(x,y,Aa,700.,1,5,0.2,function onTestRepeater)
        //set sd = ShapeData.createPentagram(x,y,a,1000.,1,5,0.,function onTestRepeater)
        set sd = ShapeData.createStarPolygon(x,y,a,3,800.,1,15,0.,function onTestRepeater)
    endfunction

    private function InitTest takes nothing returns nothing
        local trigger t = CreateTrigger()
        call TriggerRegisterUnitEvent(t,gg_unit_hfoo_0001,EVENT_UNIT_ISSUED_POINT_ORDER)
        call TriggerAddAction(t,function onTest)    
    endfunction

endscope
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top