Snippet Beta: Circles

Dirac

22710180
Reaction score
147
Hello i coded this and i thought it might be useful.
JASS:
library TCircles
//**************************************************************************************************************************
//Snippet made by Dirac to avoid polar offset positioning
//  This creates a circle around a point with a base angle 0. this angle determines the offset location
//  of the point you might need to move or create something.

//Variables:
//  real x
//      -center X
//  real y
//      -center Y
//  real radius
//      -radius of the circle
//  real xyangle
//      -current angle retrieved in radians
//  real ZRotate
//      -rotation of the circle in it's Z axis.
//  real offsetX
//      -the X depending on the circle's radius and current angle
//  real offsetY
//      -the Y depending on the circle's radius and current angle
//  real offsetZ
//      -the Z depending on the circle's height.

//Uses:
//  static method create takes real x, real y, real radius returns thistype
//          -Imput here the center x,y and radius, default angle is 0, you can change that later

//  method radiusVector takes real howMuch, real radians returns nothing
//          -This method reduces the radius size by a certain amount towards an angle
//          -The angle is measured starting from the edge of the circle, a few examples:
//            if the angle provided is 0 the radius would increase exactly by the amount provided,
//            if the angle provided is 180 the radius would decrease exactly by the amount provided,
//            if the angle provided is 90 the radius would increase sightly

//  method moveVector takes real howMuch, boolean clockwise returns nothing
//          -This method moves the circle angle around it's radius depending on the amount provided
//          -This is a very good way to avoid polar offset when you wan't to move something exactly
//          by an amount of distance around a point, not an angle.

//  method moveZVector takes real howMuch, boolean clockwise returns nothing
//          -This method moves the circle angle around it's radius on the Z axis depending on the amount provided

//  method setBase takes real radians returns nothing
//          -This method modifies the base angle of the circle, moving the offset location.

//**************************************************************************************************************************
    struct circle
        real x
        real y
        real z
        real radius
        
        private real xCos
        private real ySin
        private real zCos
        private real zSin
        private real base
        
        static method create takes real x, real y,real z, real radius returns thistype
            local thistype this=thistype.allocate()
            set .x=x
            set .y=y
            set .z=z
            set .xCos=1.
            set .ySin=0.
            set .zCos=1.
            set .zSin=0.
            set .base=0.
            set .radius=radius
            return this
        endmethod
        
        method setBase takes real radians returns nothing
            local real prev=.base
            set .base=radians
            set .xyangle=.xyangle+.base-prev
        endmethod
        
        method operator xyangle takes nothing returns real
            return Atan2(.ySin,.xCos)
        endmethod
        
        method operator xyangle= takes real radians returns nothing
            set .xCos=Cos(radians)
            set .ySin=Sin(radians)
        endmethod
        
        method operator offsetZ takes nothing returns real
            return .z+.radius*zSin
        endmethod
        
        method operator zRotate= takes real radians returns nothing
            set .zCos=Cos(radians)
            set .zSin=Sin(radians)
        endmethod
        
        method operator offsetX takes nothing returns real
            return .xCos*.radius*zCos+.x
        endmethod
        
        method operator offsetY takes nothing returns real
            return .ySin*.radius*zCos+.y
        endmethod

        method radiusVector takes real howMuch,real radians returns nothing
            local real A=.radius
            local real staticCos=.xCos
            local real staticSin=.ySin
            set .radius=SquareRoot(.radius*.radius+howMuch*howMuch-2*howMuch*.radius*Cos(bj_PI-radians))
            set .xCos=Cos(Atan2(staticSin,staticCos)+Acos((.radius*.radius+A*A-howMuch*howMuch)/(2*.radius*A)))
            set .ySin=Sin(Atan2(staticSin,staticCos)+Acos((.radius*.radius+A*A-howMuch*howMuch)/(2*.radius*A)))
        endmethod
        
        method moveVector takes real howMuch, boolean clockwise returns nothing
            if clockwise==false then
                set .xCos=.xCos-.ySin*2*howMuch/(bj_PI*.radius)
                set .ySin=.ySin+.xCos*2*howMuch/(bj_PI*.radius)
            else
                set .xCos=.xCos+.ySin*2*howMuch/(bj_PI*.radius)
                set .ySin=.ySin-.xCos*2*howMuch/(bj_PI*.radius)
            endif
        endmethod
        
        method moveZVector takes real howMuch, boolean clockwise returns nothing
            if clockwise==false then
                set .zCos=.zCos-.zSin*2*howMuch/(bj_PI*.radius)
                set .zSin=.zSin+.zCos*2*howMuch/(bj_PI*.radius)
            else
                set .zCos=.zCos+.zSin*2*howMuch/(bj_PI*.radius)
                set .zSin=.zSin-.zCos*2*howMuch/(bj_PI*.radius)
            endif
        endmethod
    endstruct
endlibrary

I consider myself good at english but when it comes to explaining stuff i'm a total wreck, so i draw these helpful demostrations to show how does the snippet works.

Here's a demostration of the "moveVector" method, the red dot shows the location you'll get if you use .getX() and .getY()


Here's a demostration of the "radiusVector" method


Demo Map? yes.
 

Attachments

  • TCircles.w3x
    59.8 KB · Views: 221

Rllulium

New Member
Reaction score
10
JASS uses radians for angles, don't you think that you should too?
To avoid unnecessary conversion.
 

tooltiperror

Super Moderator
Reaction score
231
Agreed. People (excluding new JASSers) generally use radians, and when people do use degrees, they have to generally convert to radians ([LJASS]bj_DEGTORAD[/LJASS])
 

Dirac

22710180
Reaction score
147
the only thing i do i my system that uses degrees is 180*bj_DEGTORAD but i guess i should just write bj_PI there (edit: already did), if you want to retrieve radians in your equations just use .xyangle as i say in the description or set .xyangle to an amount of radians. The angle operator was just added to make it more user friendly. I was thinking that maybe i would add if the system gets enough feedback and use, ellipses and spheres, but i'm really lost when it comes to drawing an ellipse with an angle in a equation : /
 

Romek

Super Moderator
Reaction score
964
The entire system itself should use radians instead of taking radians, converting to degrees to store, and then back again to use/return.

JASS:
//
            if SquareRoot(x*x+y*y)<currentdata.radius*currentdata.radius then
                return true
            endif
            return false

->
JASS:
return SquareRoot(x*x+y*y)<currentdata.radius*currentdata.radius

(I don't think that SquareRoot function should even be there, since you're squaring the radius on the other side of the operator)

JASS:
//
            call GroupClear(GROUP)
            call GroupEnumUnitsInRect(GROUP,GetWorldBounds(),function thistype.check)

Group enum functions already clear the group before adding new units.

You should use [ljass]bj_mapInitialPlayableArea[/ljass] instead of [ljass]GetWorldBounds()[/ljass] when selecting all units within the map.
 

Dirac

22710180
Reaction score
147
Thanks Romek i'll do some improvement there

as i'm trying to say my system DOES take radians, i just added the angle opeator to make it more user friendly:
circlestruct.xyangle = returns radians
circlestruct.angle = returns angles

Oh and i'll check that right away Nestharus, this is the way i've always drawn circles

EDIT: i'm loving the feedback
 

Dirac

22710180
Reaction score
147
Ok i made an update since radiusVector was really really really broken, now it works properly.

EDIT: also do does anyone else think this is ready for submission?
 

Dirac

22710180
Reaction score
147
i have to agree, i'm thinking of giving it 3d features, might include 3d ellipses. Also, updated the snippet to a version that no longer requires any kind of offset from the circle's center, lots of equations, i hope that's what you meant with the center polarization.

Fully tested, works great
 

Nestharus

o-o
Reaction score
84
Ok, here's what I want you to do..

create a unit at position 300,300 and move it 32x a second with this equation-
JASS:

set x=GetWidgetX(u)
set y=GetWidgetY(u)
call SetUnitX(u,x-y*.03125*.03125)
call SetUnitY(u,y+x*.03125*.03125)


That will move it in a circle around 0,0... now I'm going to leave it up to you to figure out how to offset it so that it's origin is wherever and so that it's radius is whatever.


The .03125*.03125 is just to slow it down.


Now, I think that what I did there is a lot less overhead than what you'd normally do with polar projections.


You learn vector fields to this level in Calculus 3 =).
 

Dirac

22710180
Reaction score
147
By experimenting with Nestharus's way i found out that bj_PI is extremely inaccurate, any fix to this?
 

Romek

Super Moderator
Reaction score
964
[ljass]bj_PI[/ljass] is a constant with the value 3.14159. It's pi to 5 decimal places - more than accurate enough for any real applications. It must be something wrong with your code.

> as i'm trying to say my system DOES take radians, i just added the angle opeator to make it more user friendly:
I'm aware of that. I'm saying that although it does take/return radians, you're converting internally to degrees to store, and then back to radians when you want to return. You should just keep everything in radians.
 

Dirac

22710180
Reaction score
147
Well i lost a chunk of my brain, but there it is: changed everything to "vector fields", happy Nestharus? :D
 

Dirac

22710180
Reaction score
147
you guys are HARD - TO - PLEASE.
anyways, just made it 3d you can now create spheres and stuff, the test map is very cool, go test it now. IT'S STILL A BETA

What needs to be done:
integrate both moveVector and moveZVector methods into one to be able to move the particle the same distance all along. Currently moves first the xy axis then the z axis
 
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