Placing a Shape Issue
-
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
-
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.
-
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
-
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. -
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.
-
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 😂
-
@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
-
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.
-
@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"
-
@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 😊👍🏻