How to do integration math in corona sdk?

Dear visitors

Does anyone know a library in corona, ideas or any other resource, where we can calculate and visualize the area between two functions (integration math example in the link below)?

https://qph.fs.quoracdn.net/main-qimg-d776d4afc3a85b602b5df944cd337892

I would love to create a tool in Corona SDK where I can visualize in graph tool manner 

a simple integration example.

Application idea:

Imagine there is rain and we want to understand how much liquid is in a uneven floor (see the pic attached).

Best regards,

Teo

  1. Corona is a game engine/sdk.

  2. You’d be better off searching the web for Lua + Integration.  Corona uses the Lua language.

Thanks for your reply, first of all!

I definitely require CORONA SDK game engine for this.

Imagine my GAME is about RAIN.

I want to measure the blue area or similarly asked: how much water is sitting into the hole?

Please someone hit me with the solution for measuring pixel areas in 2D in Corona SDK.

You missed my point.

  1. Measuring pixel area (I assume by counting colored pixels) won’t be the same (close but not as accurate) as actually doing the math.

  2. You will need to implement you solution in Lua, so you should include the term Lua in your searches, not Corona.

  3. You may need Corona to render your graph, but Corona doesn’t have any features to determine the ‘pixel areas’ as you say above.

Note: You can sample the color of a pixel, but doing so over and over is slow, so that is not a solution.

Hi.

How general will your functions be? Will there always be two of them, with one always being a horizontal line? Or at least with the relationship always being vertical?

Even in your example, there are three shapes resulting from your intersection: the blue part in the middle, but also the two open triangle-ish bits. How would you choose which of these matters?

If you can select the relevant part of the shape, say by clicking on it, you could paint your curves (and probably also the edges of the box) into a grid-type data structure and then flood fill (starting at the click point) until you hit those boundaries. Along the way you count up visited cells, then report the total at the end. Getting curves without gaps might also be an issue, of course.

You might also want to scale the grid (and thus the result). One reason would be any zoom you apply. The grid might be pretty blocky near curvy edges, as well, in which case sub-pixel resolution could be necessary for sufficient accuracy.

In the line-and-function case some more direct ways to do the computation might open up.

“1. Measuring pixel area (I assume by counting colored pixels) won’t be the same (close but not as accurate) as actually doing the math.”

I don’t require perfect or accurate even but just close. Let’s call it “How many Pixels are in the area?”

So, how to count the pixels in the easiest way?

There is simply no easy way to count pixels that doesn’t take a long time (wall clock time).

You can sample the color of screen pixels one by one, but that will take forever…

https://docs.coronalabs.com/api/library/display/colorSample.html

You are far better off implementing a math solver that can calculate the value you want for the equations you’re putting in.

There have to be Lua based solvers for equations out there.

I tried this search ‘lua area under the curve solver’ and found various results/starting points for research.

I think the real question here isn’t how do I solve for the area under the curve, but instead how do I render it.   That is far harder.

PS - I read some later posts and wanted to update my response here instead of re-respond later.

When I say you can Google a solution above, I assume you know the exact category of equations you are solving for. 

As @davebollinger implies later, a general search would be useless without that information.  However, I must assume you  know what kinds of problems you’re solving otherwise this is a non-starter.

Knowing exactly the category of problems you’re solving for I think the odds are high you will be able to find a ready-made library that takes inputs and produces outputs based on the equation type.  Whether this will be written in Lua or not is a hard to say.  However, assuming you are an experienced programmer you can also search in a language you know then convert to Lua.  There are many options here.

PPS - If you are solving for an extremely limited set of equations and if you know the math, you should also be able to write your own solver here.

As @roaminggamer already stated, you can solve the blue area by solving the area under the curve, just google that and you’ll find the formula(s). Solving it mathematically will yield the fastest running and most accurate result.

For rendering the graph, you’d need to determine the style and accuracy of the graph. For instance, if you plan to create a 800px wide continuous line graph, then you’d need to solve the Y for each X on the line and you’d probably want to create a display.newLine connecting each point to the next.

The real question here, in my opinion, is: what are you using the information for, i.e. why do you need to solve it? Does it hold some specific gameplay related value, like in Disney’s “Where’s My Water?” where the player has to get enough water into the end-zone to clear a level? Depending on your reason, there may be numerous alternative approaches to this issue.

you won’t find a simple analytic solution by just googling it, because there remain too many unknowns.  (already noted by starcrunch, but perhaps overloooked)

what you’d need to know before even beginning an analytic integration is:  what are the values of those two real roots you’d be integrating between?  fe, are a,b,c,d fixed constants rather than variables?  if you can somehow KNOW those two roots (there might only be two, but what if 1 or 3, or none (if you allow a=0), then what??), then several approaches might work, ottomh…

1 - would you know how to reword the problem so that you’re solving for 0 instead of 2? (hint:  d=d-what?)

(otoh, if not, then you might want to reconsider tackling this problem)

2 - would you then know how to find its real roots?  (always this cubic? if so, hint, else hint)

3a - would you then know how to get the antiderivative (indefinite integral, hint)?  if so, just integrate it between the two roots.

3b - or manually (ie iterative computation) do a finite sum where dx=1 pixel between the roots.  (or approximate the curve with a polygon generated from those same dx=1 vertices and calculate its signed area - it’ll be more computation overall, but perhaps conceptually simpler?)

  1. Corona is a game engine/sdk.

  2. You’d be better off searching the web for Lua + Integration.  Corona uses the Lua language.

Thanks for your reply, first of all!

I definitely require CORONA SDK game engine for this.

Imagine my GAME is about RAIN.

I want to measure the blue area or similarly asked: how much water is sitting into the hole?

Please someone hit me with the solution for measuring pixel areas in 2D in Corona SDK.

You missed my point.

  1. Measuring pixel area (I assume by counting colored pixels) won’t be the same (close but not as accurate) as actually doing the math.

  2. You will need to implement you solution in Lua, so you should include the term Lua in your searches, not Corona.

  3. You may need Corona to render your graph, but Corona doesn’t have any features to determine the ‘pixel areas’ as you say above.

Note: You can sample the color of a pixel, but doing so over and over is slow, so that is not a solution.

Hi.

How general will your functions be? Will there always be two of them, with one always being a horizontal line? Or at least with the relationship always being vertical?

Even in your example, there are three shapes resulting from your intersection: the blue part in the middle, but also the two open triangle-ish bits. How would you choose which of these matters?

If you can select the relevant part of the shape, say by clicking on it, you could paint your curves (and probably also the edges of the box) into a grid-type data structure and then flood fill (starting at the click point) until you hit those boundaries. Along the way you count up visited cells, then report the total at the end. Getting curves without gaps might also be an issue, of course.

You might also want to scale the grid (and thus the result). One reason would be any zoom you apply. The grid might be pretty blocky near curvy edges, as well, in which case sub-pixel resolution could be necessary for sufficient accuracy.

In the line-and-function case some more direct ways to do the computation might open up.

“1. Measuring pixel area (I assume by counting colored pixels) won’t be the same (close but not as accurate) as actually doing the math.”

I don’t require perfect or accurate even but just close. Let’s call it “How many Pixels are in the area?”

So, how to count the pixels in the easiest way?

There is simply no easy way to count pixels that doesn’t take a long time (wall clock time).

You can sample the color of screen pixels one by one, but that will take forever…

https://docs.coronalabs.com/api/library/display/colorSample.html

You are far better off implementing a math solver that can calculate the value you want for the equations you’re putting in.

There have to be Lua based solvers for equations out there.

I tried this search ‘lua area under the curve solver’ and found various results/starting points for research.

I think the real question here isn’t how do I solve for the area under the curve, but instead how do I render it.   That is far harder.

PS - I read some later posts and wanted to update my response here instead of re-respond later.

When I say you can Google a solution above, I assume you know the exact category of equations you are solving for. 

As @davebollinger implies later, a general search would be useless without that information.  However, I must assume you  know what kinds of problems you’re solving otherwise this is a non-starter.

Knowing exactly the category of problems you’re solving for I think the odds are high you will be able to find a ready-made library that takes inputs and produces outputs based on the equation type.  Whether this will be written in Lua or not is a hard to say.  However, assuming you are an experienced programmer you can also search in a language you know then convert to Lua.  There are many options here.

PPS - If you are solving for an extremely limited set of equations and if you know the math, you should also be able to write your own solver here.

As @roaminggamer already stated, you can solve the blue area by solving the area under the curve, just google that and you’ll find the formula(s). Solving it mathematically will yield the fastest running and most accurate result.

For rendering the graph, you’d need to determine the style and accuracy of the graph. For instance, if you plan to create a 800px wide continuous line graph, then you’d need to solve the Y for each X on the line and you’d probably want to create a display.newLine connecting each point to the next.

The real question here, in my opinion, is: what are you using the information for, i.e. why do you need to solve it? Does it hold some specific gameplay related value, like in Disney’s “Where’s My Water?” where the player has to get enough water into the end-zone to clear a level? Depending on your reason, there may be numerous alternative approaches to this issue.

you won’t find a simple analytic solution by just googling it, because there remain too many unknowns.  (already noted by starcrunch, but perhaps overloooked)

what you’d need to know before even beginning an analytic integration is:  what are the values of those two real roots you’d be integrating between?  fe, are a,b,c,d fixed constants rather than variables?  if you can somehow KNOW those two roots (there might only be two, but what if 1 or 3, or none (if you allow a=0), then what??), then several approaches might work, ottomh…

1 - would you know how to reword the problem so that you’re solving for 0 instead of 2? (hint:  d=d-what?)

(otoh, if not, then you might want to reconsider tackling this problem)

2 - would you then know how to find its real roots?  (always this cubic? if so, hint, else hint)

3a - would you then know how to get the antiderivative (indefinite integral, hint)?  if so, just integrate it between the two roots.

3b - or manually (ie iterative computation) do a finite sum where dx=1 pixel between the roots.  (or approximate the curve with a polygon generated from those same dx=1 vertices and calculate its signed area - it’ll be more computation overall, but perhaps conceptually simpler?)