Navigation

    Fuze Arena Logo
    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    • Help
    • Discord

    Placing a Shape Issue

    Beginners
    3
    10
    507
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J
      jbmb2000 last edited by

      Hi Guys,

      I'm working on my first project & noticing an oddity but not sure if I'm just doing something wrong.

      Basically I'm making a program were pressing buttons will add a shape, rotate all shapes, change colour etc.

      It's for my niece who's just 18 months so can't really play games like her brothers but likes when she does something & it makes soenthign else happen.

      I'm starting off with just a square (well a 4 sided circle). Where I'm running into trouble is with the placement of the shape. I want the screen to be a boundary, soni don't want a shape going off the screen. I had been trying to be clever with the random function & adding/subtracting the difference to take into account the range that would have part of the shape off screen.

      I thought I was just getting my maths messed up so went instead for the less elegant while loop solution but I still get shapes that go off screen.

      I had though that my issue was because I hadn't taken into account that the hypotenuse when splitting the square into 2 equal right angle triangles would be bigger the a side but it still didn't fix the issue.

      I decided to have a button cause the shape to be drawn at 0+(size/2) & I still had some off screen. I've mess about with it & it seems that I get the top left corner of the shape in the top left corner of the screen when I add 19 (to both the x & y value).

      Am I doing something very wrong & /or silly here (I know I can just use the 19 to sort everything out but want to try understand what's going on) ? Truncated code below (sorry about the formatting, I'm on my phone) :

      loop
         screenWidth=gwidth() 
         screenHeight=gheight() 
      
         Size=screenwidth/15
      
         c=controls(0)
      
         if !c_old.r and c.r then
            Shapes[Shapecount] =
                [
                   . Xpos=size/2+19,
                  . Ypos=size/2+19,
                 . Size=size, 
                  . Colour={getcolournumber(), getcolournunber(), getcolournunber(), 1}
                 . Rotation=45
                ] 
      
               Ashape=createcircle(shapes[shapecount]. Xpos, shapes[shapecount].ypos, shapes[shapecount].size, shapes[shapecount].sides) 
               Setshapecolour(ashape, shapes[shapecount].colour)
               Rotateshape(ashape, shapes[shapecount].rotation)
               Drawshape(ashape) 
      
              Shapecount += 1
        endif
      
         C_old=c
         Update() 
      Repeat
      
      1 Reply Last reply Reply Quote 0
      • R
        Richard F last edited by

        I might be wrong, but isn't your size variable the radius of the circle, so if you set your Xpos to size it should be just on the left side of the screen and the same idea for Ypos at the top of the screen.

        1 Reply Last reply Reply Quote 2
        • J
          jbmb2000 last edited by

          Hi Richard,

          Thanks for your reply.

          Yes, I think you are correct & I've both badly named my variable & made a very silly mistake with my calculations 😱

          I've amended the xpos & ypos to just be size but now there's a gap between the sides of the screen & my shape!

          I'm gonna have to go through my code again, I reckon I've made another ridiculous mistake somewhere

          1 Reply Last reply Reply Quote 1
          • CasualTie5
            CasualTie5 F last edited by

            I'd argue there are no ridiculous mistakes so long as we learn from them!
            and about naming your variables: my personal experience is that having longer, more precise variable names, even though annoying at first, pays off big time in the end. Sometimes it may even be a good idea to make structures within structures, to help categorize variables etc.

            1 Reply Last reply Reply Quote 2
            • R
              Richard F last edited by

              I think the gap is probably the result of the rotation. If you set the angle to 0 or any multiple of 90 the corners should touch the top and the left.

              J 2 Replies Last reply Reply Quote 1
              • J
                jbmb2000 last edited by jbmb2000

                Thanks CasualTie5, but maths was my best & favourite subject in school & I even went & did it in college, been a long time but still shouldn't make a rudimentry mistake like getting radius & diameter mixed up!

                Completely agree with making variable names precise, that's what I thought I'd done 😂😭. I've now renamed size to radius!

                Still not seeing where I've gone wrong but hopefully I'll have an epiphany & figure out was basic error I've made this time!

                Edit: had should instead of shouldn't, good to know my English is still worse then my maths 😂

                1 Reply Last reply Reply Quote 1
                • J
                  jbmb2000 @Richard last edited by

                  @richard No that's not it, I actually got this bit right 😂. 0 rotation puts the square "standing" on a corner so had to put it to 45 to get it "standing" on a side

                  1 Reply Last reply Reply Quote 0
                  • R
                    Richard F last edited by Richard

                    Yes, but your radius is the distance to each corner from the middle, which is greater than the perpendicular distance from the edge to the middle, so when you have it "standing on a side" there will be a gap at the side of the screen, which I believe will disappear if you "stand it on its corner". Using trigonometry you could calculate that distance, but I think it would probably be lost on an 18 month old.

                    J 1 Reply Last reply Reply Quote 1
                    • J
                      jbmb2000 @Richard last edited by jbmb2000

                      @richard said in Placing a Shape Issue:

                      Yes, but your radius is the distance to each corner from the middle, which is greater than the perpendicular distance from the edge to the middle, so when you have it "standing on a side" there will be a gap at the side of the screen, which I believe will disappear if you "stand it on its corner". Using trigonometry you could calculate that distance, but I think it would probably be lost on an 18 month old.

                      Ah I think you're on to something here! I thought the radius for a square would be half the width/height! I'm away from my switch at the minute but will try this as soon as I can! I'll work out the trigonometry, had done it already but backwards.

                      Thanks a mill Richard 😊👍🏻

                      Edit: fixed "autocorrect"

                      1 Reply Last reply Reply Quote 1
                      • J
                        jbmb2000 @Richard last edited by

                        @richard said in Placing a Shape Issue:

                        I think the gap is probably the result of the rotation. If you set the angle to 0 or any multiple of 90 the corners should touch the top and the left.

                        I completely misread this, got what you meant with your next post & just tested & you're completely right.

                        Thanks a mill for your help Richard 😊👍🏻

                        1 Reply Last reply Reply Quote 1
                        • First post
                          Last post