Skip to main content
deleted 65 characters in body; edited tags; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Refactor this function that moves Moving circles along a ray to eliminate location check

I have a function that moves circles out of the way of an expanding circle. It moves them along a ray from the center of the expanding circle through the center of the circle to move. This allows all surrounding circles to move without overlapping. 

Here's a diagram to better explain this:

You can see a working demo here: http://jsfiddle.net/ThinkingStiff/uLu7v/jsFiddle

And here's the code that moves the circles:

Refactor this function that moves circles along a ray to eliminate location check

I have a function that moves circles out of the way of an expanding circle. It moves them along a ray from the center of the expanding circle through the center of the circle to move. This allows all surrounding circles to move without overlapping. Here's a diagram to better explain this:

You can see a working demo here: http://jsfiddle.net/ThinkingStiff/uLu7v/

And here's the code that moves the circles:

Moving circles along a ray to eliminate location check

I have a function that moves circles out of the way of an expanding circle. It moves them along a ray from the center of the expanding circle through the center of the circle to move. This allows all surrounding circles to move without overlapping. 

Here's a diagram to better explain this:

jsFiddle

added 18 characters in body
Source Link

I expand the size of the red circle from 100px to 200px. I want to move all the blue circles out of the way by half the difference of 100px( (200-100)/2 = 50px, in this example, ) along the gray line (with the gray line being different for each blue circle).

I expand the size of the red circle from 100px to 200px. I want to move all the blue circles out of the way by the difference of 100px, in this example, along the gray line (with the gray line being different for each blue circle).

I expand the size of the red circle from 100px to 200px. I want to move all the blue circles out of the way by half the difference ( (200-100)/2 = 50px, in this example ) along the gray line (with the gray line being different for each blue circle).

Tweeted twitter.com/#!/StackCodeReview/status/146822835074121728
deleted 100 characters in body
Source Link

And it's not just that I'm using a ternary operator, as I could easily switch to an if statement. What bugs me is that I'm having to do the check at all. Those two lines are basically saying, "If the circle is on the left/top do it one way, and if it's on the right/bottom do it another way". I feel like I'm missing something earlier in the function that could be solved by switching the sides of operators, adding instead of subtracting, or getting the absolute value.

And it's not just that I'm using a ternary operator, as I could easily switch to an if statement. What bugs me is that I'm having to do the check at all. Those two lines are basically saying, "If the circle is on the left/top do it one way, and if it's on the right/bottom do it another way". I feel like I'm missing something earlier in the function that could be solved by switching the sides of operators, adding instead of subtracting, or getting the absolute value.

What bugs me is that I'm having to do the check at all. Those two lines are basically saying, "If the circle is on the left/top do it one way, and if it's on the right/bottom do it another way". I feel like I'm missing something earlier in the function that could be solved by switching the sides of operators, adding instead of subtracting, or getting the absolute value.

added 63 characters in body
Source Link
Loading
Source Link
Loading