Main Menu
  • Welcome to The RPG Maker Resource Kit.

Centering

Started by TDS, June 17, 2011, 07:19:43 AM

0 Members and 1 Guest are viewing this topic.

TDS

I have a small question about a math problem that has been bothering me for a long time.

I'm not very good at math and I believe I've been approaching this problem the wrong way, so I wanted to know if anyone else have had this problem and what was the answer that they found.

The problem:

I have a possible infinite amount of "Square plates" all of the same size let's say 32 x 32, which need to fit between point A and point B without going over.

Example:

3 "Squares Plate" (Size: 32 x 32)

0       100
A - - - B

To reach the closest number to B without going over I have to multiply the amount of plates by their size (3 * 32) = 96 and to properly center it I would use the remainder ((100 - (3 * 32)) / 2) + (3 * 32) = 98.

Now here is where my brain fries.

10 "Square Plates" (Size: 32 x 32)

0       100
A - - - B


I need to know how much I should overlap the plates to reach the closest number to point B without going over.

And that's pretty much it.

Thank you very much for reading and have a nice day.

modern algebra

Well, if you're overlapping them, then the smallest amount you could do it with is 4 plates and 4*32 is 128. You have three overlapping parts, and you need them to cover the extra 28 pixels, so 28 / 3 = 9.33333. I might be missing something since it's early, but I think that's your answer.

TDS

Thank you for your Reply modern algebra.

I gave it a go, but I think that my own calculations for it are slightly off.

While it works for 3 and 4 plates it starts to go over the limit at 6 plates.

I made an example using a simple sprite to represent the plate and borders.


#==============================================================================
# ** Square Sprite
#------------------------------------------------------------------------------
# Testing Sprite
#==============================================================================

class Square_Sprite < Sprite
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    super(nil)
    # Make Bitmap
    self.bitmap = Bitmap.new(32, 32)
    self.bitmap.fill_rect(0, 0, 32, 32, Color.new(0, 255, 0, 255))
    self.bitmap.clear_rect(1, 1, 30, 30)
  end
  #--------------------------------------------------------------------------
  # * Dispose
  #--------------------------------------------------------------------------
  def dispose
    super
  end
end


# Background Sprite
@background = Sprite.new
@background.bitmap = Bitmap.new(544, 416)
# Draw Borders
@background.bitmap.draw_text(190, 0, 32, 416, "0", 1)
@background.bitmap.fill_rect(222, 0, 1, 416, Color.new(255, 0, 0, 255))
@background.bitmap.fill_rect(322, 0, 1, 416, Color.new(255, 0, 0, 255))
@background.bitmap.draw_text(325, 0, 50, 416, "100", 1)

# Plates Array
@plates = []
# Max Number of plates
plate_n = 5

for i in 0...plate_n
  # Make Plate Sprite
  @plates[i] = Square_Sprite.new
  @plates[i].x = 222 + i * (32 - ((plate_n * 32) - 100) / (plate_n-1))
  @plates[i].y = 200 + i
end

loop do
  # Update Graphics
  Graphics.update
end



This is the line to change the number of plates to center around point A and B.


plate_n = 5



modern algebra

You'll want to do the calculations with a float, as integers will introduce some errors in the calculation. So you could just change:



  @plates[i].x = 222 + i * (32 - ((plate_n * 32) - 100) / (plate_n-1))


to:



  @plates[i].x = (222 + i * (32 - ((plate_n * 32) - 100) / (plate_n-1).to_f)).to_i


I would also recommend doing the multiplier calculation:
(32 - ((plate_n * 32) - 100) / (plate_n-1).to_f)

outside of the loop since it will always be the same and there's no real reason to keep repeating that operation. That said, it won't make a real difference unless plate_n is really large.

TDS

Ah modern algebra. I really need more friends like you.

Thank you very much for your help.

I was doing the multiplier calculation inside the loop in a strange attempt to understand the math behind it by noting the difference in values of space for each calculation.