lundi 2 avril 2018

C Algorithm into Code: Coordinates within a certain parameter

Here is some pseudocode. I understand how to do everything except Im unsure of what the condition should be to check if the randomly generated coordinate is within the circle.

For example with circle of radius 1, (-1, 1) would not fall in the circle, (-1, 0.5) would though.


numDartsInCircle = 0

repeat 1000 times

throw a dart (generate (x,y), where -1 ≤ x ≤ 1, -1 ≤ y ≤ 1)

if dart is in circle

numDartsInCircle++

fractionOfDartsInCircle = numDartsInCircle / 1000

pi ≅ fractionOfDartsInCircle * 4

Aucun commentaire:

Enregistrer un commentaire