RMRK is retiring.
Registration is disabled. The site will remain online, but eventually become a read-only archive. More information.

RMRK.net has nothing to do with Blockchains, Cryptocurrency or NFTs. We have been around since the early 2000s, but there is a new group using the RMRK name that deals with those things. We have nothing to do with them.
NFTs are a scam, and if somebody is trying to persuade you to buy or invest in crypto/blockchain/NFT content, please turn them down and save your money. See this video for more information.
Centering

0 Members and 1 Guest are viewing this topic.

pokeball TDSOffline
***
Rep:
Level 84
-T D S-
Silver - GIAW 11 (Hard)Silver - Game In A Week VII
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.

*
Rep:
Level 97
2014 Most Unsung Member2014 Best RPG Maker User - Engine2013 Best RPG Maker User (Scripting)2012 Best Member2012 Best RPG Maker User (Scripting)2012 Favorite Staff Member2012 Most Mature MemberSecret Santa 2012 ParticipantProject of the Month winner for July 20092011 Best Use of Avatar and Signature Space2011 Best RPG Maker User (Scripting)2011 Most Mature Member2011 Favourite Staff Member2011 Best Veteran2010 Most Mature Member2010 Favourite Staff Member
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.

pokeball TDSOffline
***
Rep:
Level 84
-T D S-
Silver - GIAW 11 (Hard)Silver - Game In A Week VII
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.

Code: [Select]
#==============================================================================
# ** 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.

Code: [Select]
plate_n = 5


*
Rep:
Level 97
2014 Most Unsung Member2014 Best RPG Maker User - Engine2013 Best RPG Maker User (Scripting)2012 Best Member2012 Best RPG Maker User (Scripting)2012 Favorite Staff Member2012 Most Mature MemberSecret Santa 2012 ParticipantProject of the Month winner for July 20092011 Best Use of Avatar and Signature Space2011 Best RPG Maker User (Scripting)2011 Most Mature Member2011 Favourite Staff Member2011 Best Veteran2010 Most Mature Member2010 Favourite Staff Member
You'll want to do the calculations with a float, as integers will introduce some errors in the calculation. So you could just change:

Code: [Select]

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

to:

Code: [Select]

  @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:
Code: [Select]
(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.

pokeball TDSOffline
***
Rep:
Level 84
-T D S-
Silver - GIAW 11 (Hard)Silver - Game In A Week VII
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.