pls hep me !!!

hi Guys

i want to draw graph using sin, cos, tan value.

pls give me some sure suggestion regarding it…

thank u all
bye. [import]uid: 179279 topic_id: 31139 reply_id: 331139[/import]

Maybe you should calculate some point of that function and draw a line between two points.
Make some tests to check how big should be the distance in pixels between two points.
[import]uid: 138389 topic_id: 31139 reply_id: 124527[/import]

Maybe you should calculate some point of that function and draw a line between two points.
Make some tests to check how big should be the distance in pixels between two points.
[import]uid: 138389 topic_id: 31139 reply_id: 124527[/import]

I don’t program in lua often enough so forgive me but here is some code I put together quickly to give you an example. It just graphs whatever function you want to the display. Uncomment the other plot lines to see the other functions. Use at your own risk :).

[code]
local _W = display.contentWidth
local _H = display.contentHeight

local graphXMin = 20
local graphXMax = _W - 20
local graphYMin = 20
local graphYMax = _H - 20
local lines = {}

local function plot(fnc, vmin, vmax)
local x,y
local fmin = fnc(vmin)
local fmax = fnc(vmax)
local vstep = (vmax-vmin)/(graphXMax-graphXMin)

– Find the functions min and max values
for j = vmin, vmax, vstep do
if(fnc(j) < fmin) then fmin = fnc(j) end
if(fnc(j) > fmax) then fmax = fnc(j) end
end

– Restrict values like tangent that can get very large
if(fmin < -10) then fmin = -10 end
if(fmax > 10) then fmax = 10 end

– Find slope and y intercept to map function values to y pixels
local my = (graphYMin-graphYMax)/(fmax-fmin)
local by = graphYMin - my * fmax

x = graphXMin
y = my*fnc(vmin) + by
local graph = display.newLine(x, y, x+.0001, y+.0001)
graph:setColor(255,255,255,255)
graph.width = 3

– loop through x coordinate values
for i = vmin+vstep, vmax, vstep do
x = x + 1
if(fnc(i) > 10) then
charterror = true
elseif (fnc(i) < -10) then
charerror = true
else
y = my * fnc(i) + by

– Previous chart error is over
if(charterror == true) then
lines[#lines+1] = graph
graph = display.newLine(x,y,x+.0001,y+.0001)
graph:setColor(255,255,255,255)
graph.width = 3
else
if(charterror == false) then
graph:append(x, y)
end
end
charterror = false
end
end
end

plot(math.sin, 0, 2*math.pi)
–plot(math.cos, 0, 2*math.pi)
–plot(math.tan, 0, 2*math.pi)
–plot(math.cosh, 0, math.pi)
–plot(math.tanh, 0, math.pi)
–plot(math.atan, -1, 1)[/code] [import]uid: 22532 topic_id: 31139 reply_id: 125512[/import]

I don’t program in lua often enough so forgive me but here is some code I put together quickly to give you an example. It just graphs whatever function you want to the display. Uncomment the other plot lines to see the other functions. Use at your own risk :).

[code]
local _W = display.contentWidth
local _H = display.contentHeight

local graphXMin = 20
local graphXMax = _W - 20
local graphYMin = 20
local graphYMax = _H - 20
local lines = {}

local function plot(fnc, vmin, vmax)
local x,y
local fmin = fnc(vmin)
local fmax = fnc(vmax)
local vstep = (vmax-vmin)/(graphXMax-graphXMin)

– Find the functions min and max values
for j = vmin, vmax, vstep do
if(fnc(j) < fmin) then fmin = fnc(j) end
if(fnc(j) > fmax) then fmax = fnc(j) end
end

– Restrict values like tangent that can get very large
if(fmin < -10) then fmin = -10 end
if(fmax > 10) then fmax = 10 end

– Find slope and y intercept to map function values to y pixels
local my = (graphYMin-graphYMax)/(fmax-fmin)
local by = graphYMin - my * fmax

x = graphXMin
y = my*fnc(vmin) + by
local graph = display.newLine(x, y, x+.0001, y+.0001)
graph:setColor(255,255,255,255)
graph.width = 3

– loop through x coordinate values
for i = vmin+vstep, vmax, vstep do
x = x + 1
if(fnc(i) > 10) then
charterror = true
elseif (fnc(i) < -10) then
charerror = true
else
y = my * fnc(i) + by

– Previous chart error is over
if(charterror == true) then
lines[#lines+1] = graph
graph = display.newLine(x,y,x+.0001,y+.0001)
graph:setColor(255,255,255,255)
graph.width = 3
else
if(charterror == false) then
graph:append(x, y)
end
end
charterror = false
end
end
end

plot(math.sin, 0, 2*math.pi)
–plot(math.cos, 0, 2*math.pi)
–plot(math.tan, 0, 2*math.pi)
–plot(math.cosh, 0, math.pi)
–plot(math.tanh, 0, math.pi)
–plot(math.atan, -1, 1)[/code] [import]uid: 22532 topic_id: 31139 reply_id: 125512[/import]