# Need to find formula

The card game I’m making I need to have the card in the player’s hand a certain number of pixels apart depending on the number of card in the hand. I have how far apart each card should be but I’m no math whiz and have no idea what formula is used for the following X axis pixel distances:

```2 = 16 pixels 3 = 12 pixels = 4 difference from 2 cards 4 = 10 pixels = 2 difference from 3 cards 5 = 3 pixels = 7 difference from 4 cards 6 = -17 pixels = 20 difference from 5 cards 7 = -31 pixels = 14 difference from 6 cards```

One card in the hand goes in the middle of my Canvas. When the player has 2 card in their hand there are 16 pixels between each card. For 3 cards the distance is 12 for a difference of 4 from having 2 cards. 4 cards make it 10 pixels between each card. For 6 and 7 cards in the hand the negative means the number of pixels the card will overlap.

I’d rather not have to hard code all the possibilities manually as there could be 15 or more cards in the hand depending on the situation. It would be much easier to use a formula that does all the work for me.

how did you determine those values? just because they “looked good”?
if so, then you might have to compromise a few pixels here or there in order to reduce it to an equation

What I do in card game programs is NOT what you seem to be doing, but pick a set value (for overlapping cards I use a value that exposes the suit/value in the cards corner, for non-overlapping I pick a value “that looks good” )

Assume your cards are cW pixels in width, and you want them NOT to overlap, but be SP pixels apart, and the center of the display is cX

``````x=cX-((cW*numCards)+(SP*(numCards-1))/2)  //  this is where the left edge of the first card goes
for I=1 to cX
draw_card(i,x,y) // assuming Y is set already
x=x+(cW+SP)
next I``````

for OVERLAPPING cards … assume visible area is V

``````x=cX+(CW+(V*numCards-1))/2
for I=1 to cX
draw_card(i,x,y) // assuming Y is set already
x=x+V
next I``````

much easier than trying to calculate a non-linear equation NOTE : this code is for illustration purposes only, and is not meant as a cut/paste solution. It is up to you to understand the process and adapt it to fit your circumstance.

I didn’t randomly choose those pixels, it’s what a similar TCG Card game used based on screenshots I took.

Basically I want the cards to get closer to each other the more cards there are in the player’s hand. The opponent’s hand is another story since those cards are being dealt from right to left instead of left to right.

When you have one card in the hand it will go in the center of the canvas at (383, 729).

if you have 1 to 7 cards … then just make a Select Case statement… again… it is easier than reverse engineering a non-linear equation

Or use an array:

``Dim Spacing() as integer = array(0,0,16,12,10,3,-17,-31)``

If you are set on an equation, use Excel to provide you an estimation. For example: y = -0.037x^3 - 1.6786x^2 + 8.1442*x + 5.9048

[quote=347709:@Robert Weaver]Or use an array:

``` Dim Spacing() as integer = array(0,0,16,12,10,3,-17,-31) ```[/quote]
That doesn’t help as there will be more than just 7 cards in the hand depending on the situation. There could be 15 or more.

I have no idea what that is. That’s way beyond my math skills.

I’ve decided for now to use a formula just just sets the cards at a specific interval. Sure I can only display 8 cards at any given time or they will be outside the visible range of the canvas but it should be enough for now.

I guess if there will be a ninth or more cards I can tell the code to decrease the space between cards to something smaller.

I’ve found this website quite helpful at times:

http://www.webmath.com/equline1.html

That was just an example. You can add the additional spacing values to the array, one value for each additional card. We can’t give you a complete answer when you only tell us part of the problem. I assume that you know the maximum number of cards that a player can possibly have. If you tell us the spacings for all of them, then we can give you a full answer.

(FYI, based on the numbers you have given us, there is no apparent simple pattern. So we can’t fit any simple formula to these numbers.)

I’ve decided to use a simple formula where card one is always in the same place and then a conditional that changes the spacing based on the number of cards in the player’s hand. Up to 8 cards = 78 pixels, 8-11 cards is 50 and more than 11 is 30. I can display up to 20 cards with this. It is unlikely that there will be more than that for now.

Thanks for the help.

I guess eventually, later on when the game is actually working I’ll rework it so the cards will move when drawn.

then you equation is
space=(total_width-(num_cards*width_of_card))/(num_cards-1)

total_width=distance horizontally that the cards must fit within (canvas.width???)
num_cards = number of cards in the hand
width_of_card = number of pixels width the graphic image of the card is

this space “N” cards evenly regardless of the number of cards