Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.15 KB | None | 0 0
  1. --[[
  2.     image:blitRotated(x,y,angle [,cx, cy])
  3.    
  4.         Blit a rotated image, where the rotation's center is the point (cx,cy)
  5.        
  6.             - x: The X-coordinate to blit the image
  7.             - y: The Y-coordinate to blit the image
  8.             - angle: The angle of the rotation, in radian
  9.             - cx: The X-coordinate of the rotation's center (where the up left corner is (0;0)), default: imageWidth/2
  10.             - cy: The Y-coordinate of the rotation's center (where the up left corner is (0;0)), default: imageHeight/2
  11.    
  12.     ]]--
  13.    
  14. local cos, sin = math.cos, math.sin
  15.    
  16. local function getCoord(point, center, a)
  17.     local t = {}
  18.     t.x = point.x*cos(a) - center.x*cos(a) - point.y*sin(a) - center.y*sin(a) + center.x;
  19.     t.y = point.x*sin(a) + center.x*sin(a) + point.y*cos(a) + center.y*cos(a) - center.y;
  20.    
  21.     return t;
  22. end
  23.    
  24. function g2d.BlitRotated(image, x, y, angle, cx, cy)
  25.     local center = {x = x + cx - image:width() / 2 or image:width() /2, y = y + image:height() / 2 - cy or image:height() / 2}
  26.     --local origin = {x = image:width() / 2, y = image:height() / 2}
  27.    
  28.     local corners = {} -- All the image's corners
  29.         corners[1] = {x = -image:width() / 2, y = image:height() / 2}
  30.         corners[2] = {x = image:width() / 2, y = image:height() / 2}
  31.         corners[3] = {x = image:width() / 2, y = -image:height() / 2}
  32.         corners[4] = {x = -image:width() / 2, y = -image:height() / 2}
  33.        
  34.     local rot = {}
  35.         for i = 1, table.getn(corners) do
  36.             rot[i] = getCoord(corners[i], center, angle)
  37.         end
  38.        
  39.     g2d.BeginQuads(image)
  40.         --g2d.SetColor(Color.create(255,0,0))
  41.         g2d.SetCoordMode(g2d.CENTER)
  42.        
  43.         for _, obj in ipairs(rot) do
  44.             g2d.SetCoordXY(obj.x + x, obj.y + y)
  45.             g2d.Add()
  46.         end
  47.     g2d.End()
  48.    
  49.     return rot
  50. end
  51.  
  52.  
  53. local hippo = g2d.TexLoad("discret1.png", g2d.SWIZZLE)
  54.  
  55. while 1 do
  56.     g2d.Clear()
  57.    
  58.     g2d.BeginRects(hippo)
  59.         g2d.SetCoordXY(240, 136)
  60.         --g2d.Add()
  61.     g2d.End()
  62.    
  63.     local t = hippo:BlitRotated(240, 136, math.rad(35), 20, 20)
  64.    
  65.     if Ctrl.read():start() then break end
  66.    
  67.     Xtream.print(300, 200, tostring(math.floor(t[1].x)).."  "..tostring(math.floor(t[1].y)))
  68.     Xtream.print(300, 215, tostring(math.floor(t[2].x)).."  "..tostring(math.floor(t[2].y)))
  69.    
  70.     g2d.Flip(g2d.VSYNC)
  71. end
  72.  
  73. -- EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement