def draw
background(200)
maxX = 2.8
maxY = 1.5
# Compute the natural log of a value between 0 and maxX
xValue = map(mouseX, 0, width, 0, maxX)
if xValue > 0
# Cannot take the log of a negative number.
yValue = log(xValue)
y = map(yValue, -maxY, maxY, height, 0)
# Display the calculation occurring.
legend = format("log(%.2f)\n=%.3f", xValue, yValue)
stroke(150)
line(mouseX, y, mouseX, height)
fill(0)
text(legend, 5, 15)
noStroke
ellipse(mouseX, y, 7, 7)
end
# Draw the log(x) curve,
# over the domain of x from 0 to maxX
noFill
stroke(0)
shape do
(0...width).each do |x|
xValue = map(x, 0, width, 0, maxX)
yValue = log(xValue)
y = map(yValue, -maxY, maxY, height, 0)
vertex(x, y)
end
end
line(0, 0, 0, height)
line(0, height / 2, width, height / 2)
end