No, but several of us have used this formula or one like it, to get the shortest distance to a line
Here is a post I wrote about the formula our mathematician in residence provided (his version isn’t quite the same because it restricts the line so it can’t go past the endpoints, ie it won’t be perpendicular in that case.
@MattthewLXXIII - if you look at the index page of my blog (from the link above) you’ll see my ebooks. They have a lot of math tricks and handy functions in them, especially the 3D grabbag book. It was really hard work finding them, so I thought I’d make it easier for the next person…
function setup()
dostuff=0
end
function draw()
background(255, 255, 255, 255)
if ElapsedTime==1 then
dostuff=dostuff+1
end
strokeWidth(5)
fill(0)
if dostuff == 1 then
ellipse(100,100,100)
end
end
If you are implementing a set of states, then it is more efficient to write it like this
states={DrawMenu,DrawSettings,DrawPlay,DrawEndOfGame}
function setup()
state=1
end
function draw()
states[state]() --run function for current state
end
--draw functions for different states
function DrawMenu()
--code to draw menu screen
--you can transition states easily
state=2 --change to Settings screen
state=3 --start playing the game
end
function DrawSettings()
--code to draw and manage settings screen
--at the end you can revert to the menu
state=1
end
This is much cleaner than having a big draw function with lots of if statements, and runs faster because you don’t need to test what state you are in, each time you draw.
Thanks Ignatz, I was using it for something else, but that will be useful too. I think I saw that on one of your links. I was just wondering how ElapsedTime works, why doesn’t it just add DeltaTime every frame?
I would expect that it does, but DeltaTime is not exactly 1/60 of a second, it depends on frame rate, so total elapsed time is probably never an integer.
isn’t ElapsedTime continually increasing? If so, most of the functions above would draw the ellipse once and once only, which will not likely be visible on the screen at all …
It will probably not occur at all because the interval between drawing is not exactly 1/60 second, so instead of 1 you will get 1.000329 or something like it.
@RonJeffries Actually, the ellipse would be drawn constantly. As ElapsedTime increases and is equal to 1, doStuff increments to 1. If doStuff is 1, the ellipse is drawn. As ElapsedTime increases, it will never be equal to 1 again, so doStuff will never increase and will stay at 1, which will continue to draw the ellipse. But this is assuming that ElapsedTime will equal 1 as it increases.
@Ignatz I was going by the way the code was originally written and assuming that ElapsedTime would equal 1. Then the ellipse would be drawn from that point forward. I’m sure if that code was run enough times, ElapsedTime would equal 1 at least once, but I’m not going to try and verify that.
Just run this code. It will print a message if ElapsedTime is ever an integer, not just 1, but 2,3,4,…
Don’t hold your breath waiting…
function setup()
parameter.text("Time","")
end
function draw()
if ElapsedTime==math.floor(ElapsedTime) then
print("success after "..ElapsedTime.." seconds")
end
Time=math.floor(ElapsedTime)
end
@Ignatz If something can happen, given enough time, it will. If your code ran long enough, or was restarted enough times, it will print success after some integer at least once. I’m not saying it would happen in my lifetime or yours or anyone’s, but it would happen. If you take a zillion coins and throw them in the air enough times, they’ll all land on heads or tails at least once. If you can create a truely random number, then every number, no matter how large or small, has the same chance of be chosen.