夜半突来兴致,看了下wxLua的图形处理部分,感觉挺好玩,就试着写了一段画正弦曲线的代码。为了实现动画效果特意添加了延迟效果,感觉延迟的效率好低,画的时候窗口拖都拖不动。
效果图:
代码部分:
代码
-- --------------------------
-- 点和线实现正弦曲线
-- wxLua支持
-- Aurhor Chuwp
-- Date 2010.10.30
-- --------------------------
require("wx")
local nMotionFlag = 0
-- -------------
-- 主函数
-- -------------
function main()
frame = wx.wxFrame( wx.NULL, wx.wxID_ANY, "wxLua Scribble",
wx.wxDefaultPosition, wx.wxSize(450, 450),
wx.wxDEFAULT_FRAME_STYLE )
if nMotionFlag == 0 then
frame:Connect(wx.wxEVT_LEFT_DOWN, OnLClick)
end
frame:Show(true)
end
-- ----------------------
-- 左键点击开始画曲线
-- ----------------------
function OnLClick(event)
nMotionFlag = 1
local drawDC = wx.wxClientDC(frame)
pen = wx.wxPen(wx.wxColour(0, 255, 0), 1, wx.wxSOLID)
drawDC:SetPen(pen)
--坐标轴X
for i=50, 300 do
--drawDC:DrawLine(1, 1, 100, 100)
if math.mod(i,2) == 0 then
drawDC:DrawPoint(50, i)
end
end
--坐标轴Y
for j=30, 400 do
if math.mod(j,2) == 0 then
drawDC:DrawPoint(j, 175)
if math.mod(j,20) == 0 then
for k = 0, 6 do
drawDC:DrawPoint(j, 175-k)
end
end
end
end
--正弦曲线
local nTempX = 50
local nTempY = 175
pen = wx.wxPen(wx.wxColour(255, 0, 0), 1, wx.wxSOLID)
drawDC:SetPen(pen)
for i=50, 400 do
delay()
local nRec = i/12
drawDC:DrawLine(nTempX, nTempY, i, 175+50*math.sin(nRec))
nTempX = i
nTempY = 175+50*math.sin(nRec)
end
drawDC:SetPen(wx.wxNullPen)
end
-- ----------------
-- 延迟处理
-- ----------------
function delay()
local x = os.clock()
while os.clock()-x <= 0.01 do end
end
main()
wx.wxGetApp():MainLoop()
-- 点和线实现正弦曲线
-- wxLua支持
-- Aurhor Chuwp
-- Date 2010.10.30
-- --------------------------
require("wx")
local nMotionFlag = 0
-- -------------
-- 主函数
-- -------------
function main()
frame = wx.wxFrame( wx.NULL, wx.wxID_ANY, "wxLua Scribble",
wx.wxDefaultPosition, wx.wxSize(450, 450),
wx.wxDEFAULT_FRAME_STYLE )
if nMotionFlag == 0 then
frame:Connect(wx.wxEVT_LEFT_DOWN, OnLClick)
end
frame:Show(true)
end
-- ----------------------
-- 左键点击开始画曲线
-- ----------------------
function OnLClick(event)
nMotionFlag = 1
local drawDC = wx.wxClientDC(frame)
pen = wx.wxPen(wx.wxColour(0, 255, 0), 1, wx.wxSOLID)
drawDC:SetPen(pen)
--坐标轴X
for i=50, 300 do
--drawDC:DrawLine(1, 1, 100, 100)
if math.mod(i,2) == 0 then
drawDC:DrawPoint(50, i)
end
end
--坐标轴Y
for j=30, 400 do
if math.mod(j,2) == 0 then
drawDC:DrawPoint(j, 175)
if math.mod(j,20) == 0 then
for k = 0, 6 do
drawDC:DrawPoint(j, 175-k)
end
end
end
end
--正弦曲线
local nTempX = 50
local nTempY = 175
pen = wx.wxPen(wx.wxColour(255, 0, 0), 1, wx.wxSOLID)
drawDC:SetPen(pen)
for i=50, 400 do
delay()
local nRec = i/12
drawDC:DrawLine(nTempX, nTempY, i, 175+50*math.sin(nRec))
nTempX = i
nTempY = 175+50*math.sin(nRec)
end
drawDC:SetPen(wx.wxNullPen)
end
-- ----------------
-- 延迟处理
-- ----------------
function delay()
local x = os.clock()
while os.clock()-x <= 0.01 do end
end
main()
wx.wxGetApp():MainLoop()
请发表评论