How to determine the center of several points
BlitzMax Forums/BlitzMax Beginners Area/How to determine the center of several points
| ||
Hi ! i'm searching a fast method to determine the center m(x,y) of several p(x,y) points on the screen. (After this i need to rotate the p(x,y) points arround M) Thanks ! |
| ||
The easiest way is to iterate through all the points, and find the lowest and highest X and Y value for each; i.e. lowX, lowY, highX, highY. You can then calculate the centre by working out the half way point between low and high;centreX = highX - ((highX-lowX) * 0.5), probably. Once you've done that, in order to rotate the points around the centre you'll nee to know two things 1: The angle from the centre to each point. You can use Atan2() to calculate that. 2: The distance from centre to each point; d# = Sqr(((x2-x1)^2) + ((y2-y1)^2)) Bit busy now but I might write the code later if I get bored/can be bothered. Last edited 2011 |
| ||
I get bored quickly: |
| ||
Very impressive by this Gfk. Nice code. Next is there a way to 'collapse' contract and 'expand' the points to the center (without rotation). You have a better math skill than myself ! Last edited 2011 |
| ||
This version includes a tMultiplier object which you control with cursor left/right. If you don't want rotation then just comment out the code that increments the angle value in turnDots(). Last edited 2011 |
| ||
Function findCentre2(centreX:Int Var, centreY:Int Var) Local Totx:Int = 0 Local Toty:Int = 0 Local Cnt:Int = 0 Local t:tPoint For t = EachIn pointList Cnt = Cnt + 1 Totx = t.x + Totx Toty = t.y + Toty Next If Cnt > 0 centreX = totx / Cnt centreY = toty / Cnt Else centreX = 0 centreY = 0 EndIf End Function Gfk You arnt finding the only centre of the Points, what you are doing is finding the centre of the most extreme points. That is if you had four points x-coor 100,40,40,0, your centre would be 50 The function above would give 45 X-coor 100,90,90,0 yours would give, 50 Mine would give 70 |
| ||
Add them all up and divide? |
| ||
Add them all up and divide? YepWell thats what mine does, (ie finds the Mean), what Gfks does is find half the range Plus the min. Both are valid "Centres" Gfks would look most correct for images, mine most correct for centres of mass, (ish, that is it assumes all dots weight the same atm)) Last edited 2011 |
| ||
There are in fact loads of ways of defining the centre, even for just three points. The centroid, H&K's one, is probably the best for what hub wants (assuming the points are fairly regularly spread out) because it will look 'balanced' when rotating. |
| ||
Many thanks for your help. I've just finished to adapt this for my game and all is ok now. Note than i use h&k method to determine the center. |
| ||
These are some really valuable snippets, I hope they find their way into a tutorial or something for posterity as I will surely have lost this post by the next time I need them. |
| ||
code archive ? |