// Sketch: Shows how to define and use a function that returns a value: distance() // By: Deepak Kumar // Date: September 19, 2022 // Each mouse click draws a line from the origin to the place where mouse // is clicked. Also, displays the computed distance (in pixels) from the origin.
void setup() { size(500, 500); background(255); } // setup()
float x = 100, y = 100; void draw() { background(255);
line(0, 0, x, y); fill(0); float d = distance(0, 0, x, y); text(d, x, y); } // draw()
float distance(float x1, float y1, float x2, float y2) { // Returns the distance between the points (x1, y1) and (x2, y2) float dx = x1 - x2; float dy = y1 - y2; return sqrt(dx*dx + dy*dy); } // distance()
void mousePressed() { x = mouseX; y = mouseY;
} // mousePressed()