forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Rounding technique required?

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development
View previous topic :: View next topic  
Author Message
KawaGeo



Joined: 27 Aug 2005
Posts: 191
Location: Calif Mountains

PostPosted: Wed Aug 31, 2005 3:38 am    Post subject: Rounding technique required? Reply with quote

Hi, everybody!

We know all Lua numbers are floats, no integers available. However, integers are required for the arguments in most Lua Player functions. Hence, rounding technique is necessary. Here is the evidence in the code.

Code:
-- circleTest.lua by Geo Massar, 2005  (aka kawageo)

PI2 = math.pi * 2

function math.round(num, dp)
  local mult = 10^(dp or 0)
  return math.floor(num  * mult + 0.5) / mult
end

function drawCircleOn(image, x0,y0, radius, segments, color)
  -- x0,y0 is the center of the circle
  local x1,y1, x2,y2
  x1, y1 = x0, y0 + radius
  for i = 1, segments do
    x2 = math.round(x0 + radius * math.sin(PI2*i/segments))
    y2 = math.round(y0 + radius * math.cos(PI2*i/segments))
    image:drawLine(x1,y1, x2,y2, color)
    x1,y1 = x2,y2
  end
end

function magnifyOn(image, mag)
  mag = mag or 2           -- 2 times in size by default
  local w = image:width() 
  local h = image:height()
  local result = Image.createEmpty(mag*w, mag*h)
  for x = 0, w-1 do
    for y = 0, h-1 do
      result:fillRect(mag*x, mag*y, mag,mag, image:pixel(x,y))
    end
  end
  return result
end

--------------------- main routine -------------------------

green = Color.new(0, 255, 0)

panel = Image.createEmpty(30, 30)

drawCircleOn(panel, 24,5,  5,  4, green)       -- a fat diamond
drawCircleOn(panel, 24,24, 5, 12, green)       -- a small circle
math.round = function(num, dp) return num end  -- no rounding
drawCircleOn(panel, 5,5,   5,  4, green)
drawCircleOn(panel, 5,24,  5, 12, green)

panel = magnifyOn(panel, 4)
left = (480 - panel:width()) / 2
top = (272 - panel:height()) / 2
screen:blit(left, top, panel)

screen.flip()
screen:save "screen.tga"

while true do
  screen.waitVblankStart()
end

The result is displayed below. The symbols on the left side are resulted without rounding. They are badly distorted whereas the symbols on the other side look perfect due to the rounding technique.



The question: Is it our responsibility to make sure all arguments are in pure integers OR should Lua Player take care of the rounding business?

NOTE: The code was tested on Lau Player for Windows. I presumed the same thing for a real PSP. (I don't have a PSP with version 1.5 yet. It is on the way, by the way. :)
_________________
Geo Massar
Retired Engineer
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group