Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Simple Circle Class — Gideros Forum

Simple Circle Class

CesarCesar Member
edited May 2016 in Code snippets
I just want to share a simple Circle class
Circle = Core.class(Shape)
 
function Circle:init(x, y, radius)
	local sides=math.floor(radius/2)
	local angleStep=360/sides
	--self:setFillStyle(Shape.SOLID, 0xff0000, 1)
	self:setLineStyle(1, 0xff0000, 0.8)
	self:beginPath()
	self:moveTo(radius, 0)
 
	for i = 1,sides do
		local angle = math.rad(i*angleStep)
		xx = math.cos(angle)*radius	
		yy = math.sin(angle)*radius	
		self:lineTo(xx,yy)	
	end
 
	self:endPath()
	self:setPosition(x,y)	
end
+1 -1 (+3 / -0 )Share on Facebook

Comments

  • antixantix Member
    @Cesar, thanks for sharing :)

    Here's a function to test if a given x,y position is inside a circle..
    function Circle:pointInCircle(x, y)
      local r = self.radius ^ 2
      if (y - self.y) ^ 2 / r + (x - self.x) ^ 2 / r <= 1 then return true end
      return false
    end
    Also, if you ever want to expand the abilities of your circle class to include collision with other circles you should checkout the following page.. http://blogs.love2d.org/content/circle-collisions, it's pretty cool stuff.
    +1 -1 (+2 / -0 )Share on Facebook
  • CesarCesar Member
    @antix I think you can use hitTestPoint() as Circle class inherits from Sprite class. Thank you anyway.
  • keszeghkeszegh Member
    @Cesar, hitTestPoint() only tests against the bounding box of the sprite, so it won't work properly for a circle.
    +1 -1 (+4 / -0 )Share on Facebook
  • talistalis Guru
    edited May 2016
    As you can see in this threat also, whenever your buttons will be round not rectangular you can have some miss clicks with normal hitTestPoint() . (As button class is inheriting sprite)
    http://giderosmobile.com/forum/discussion/3595/non-rectangle-button/p1

    Likes: Cesar

    +1 -1 (+1 / -0 )Share on Facebook
  • CesarCesar Member
    edited May 2016
    Thank you very much for the observation

    Likes: antix

    +1 -1 (+1 / -0 )Share on Facebook
  • antixantix Member
    edited May 2016
    @Cesar, Also if you are going to make many circles you could optimize things a little..
    	for i = 1,sides do
                    local rad, sin, cos = math.rad, math.sin, math.cos
    		local angle = rad(i*angleStep)
    		local xx = cos(angle)*radius	
    		local yy = sin(angle)*radius	
    		self:lineTo(xx,yy)	
    	end
    Making things local makes a difference :)

    Likes: rolfpancake

    +1 -1 (+1 / -0 )Share on Facebook
  • chuz0chuz0 Member
    For round buttons I use the Vector2 class; I just check the e.x,e.y:distanceTo(center_of_button), if it's shorter than the radius it's a hit.
  • SinisterSoftSinisterSoft Maintainer
    @antix, will not making the functions local to the function rather than local to the loop be faster - I've not checked what bytecode will be produced.

    Likes: antix

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
    +1 -1 (+1 / -0 )Share on Facebook
  • antixantix Member
    @chuz0, care to share your solution?

    @SinisterSoft, you are most likely right. Hmm, I have MANY things to change in my codebase :D

    Likes: SinisterSoft

    +1 -1 (+1 / -0 )Share on Facebook
  • SinisterSoftSinisterSoft Maintainer
    I'm not sure if making things local to the app is the same speed as local to the function. It may be.
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • chuz0chuz0 Member
    edited May 2016
    @antix, sure, take a look here : http://giderosmobile.com/tools/vector2
    I've used the method in the past, and probably it's not the most effective solution but it worked :D

    Something like this :
    game = Core.class(Sprite)
      function game:init()
     
      button = Bitmap.new(Texture.new("roundbutton.png",true))
      button:setAnchorPoint(.5,.5)
      button:setPosition(100,100)
     
     
      self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self)
     
      end
     
    function game:onMouseUp(event)
      local v = Vector2.new(e.x,e.y)
      if v:distanceTo(button:getX(),button:getY())<=button:getWidth()/2 then
          print("button touched")
      end
     
    end
    I haven't tried but it should work.

    Likes: antix

    +1 -1 (+1 / -0 )Share on Facebook
  • antixantix Member
    @chuz0 cheers, I had not previously seen the vector2 library . I still will stick with mine since it doesn't need to do a math.sqrt ;)
Sign In or Register to comment.