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.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top