An assortment of questions.

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.

https://coolcodea.wordpress.com/2015/01/18/193-shortest-distance-from-a-point-to-a-line/

@Ignatz, thanks, his version is nicer.

@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…

Another question guys:

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

Why doesn’t the ellipse ever draw?

Lack of precision. ElapsedTime probably won’t ever equal 1 exactly. Try this

    if dostuff==0 and ElapsedTime>1 then dostuff=dostuff+1

I put the dostuff test first because it is (probably) faster than looking up ElapsedTime

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.

If you want ElapsedTime to be an integer, just do

    ElapsedTime//1
or
    math.floor(ElapsedTime)

However, I wouldn’t use this approach simply to tell you if one second has passed

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 …

Yes it is, but it is used to turn on an indicator, which is what decides if the ellipse is drawn

ISTM that ElapsedTime == 1 will occur at most once.

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.

right, thus “at most once” :smile:

@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.

ElapsedTime won’t ever exactly equal 1, that’s why I used a > test.

@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.

I don’t know how many times I need to say this.

ElapsedTime is NEVER likely to equal 1.

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.