Skip to content
This repository has been archived by the owner on Oct 9, 2018. It is now read-only.

Conditionals and Looping 2

dimituri edited this page Oct 2, 2010 · 2 revisions

For the following problem you need to know some mathematical operations in Ruby. Namely, exponentiation.

In Ruby, (x^n) is written as x ** n.

It is known that for a positive number, its square root can be written in exponent notation: (\sqrt[n]{x} = x^{1/n}). So, to calculate the principal square root of x you can do x ** 0.5.

2 ** 4 # => 16
9 ** 0.5 # => 3.0

Problem

You are given two points:

  1. with coordinates 230, 240
  2. with coordinates 410, 240

Draw all pixels for which the distance to any one of the two given points is less than to 150.

Use the Pythagorean theorem to calculate distances, where legs are equal to the horizontal and vertical difference between points.

[\Delta = \sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}]

Result

Try to solve the problem on your own and guess what the result should look like, before running it. Then compare with [the correct result](Conditionals and Looping 2 (Result)).