Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch edge case and fix example #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,13 @@ const Circle = stampit(Point, {
},
methods: { // that goes to the prototype
distance(point) {
return Point(point).distance(this) - this.radius;
// Shouldn't this only return a value if the point is located outside of the circle?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the tips mate.
This function returns positive value if the point is outside the circle. But returns negative value if the point is inside.

// Otherwise, we can end up with negative distances.
// This is assuming we want the distance between the point and the closest location on the circle.
// Proposed solution:
const distanceFromCenterpoint = Point(point).distance(this);
const finalDistance = (distanceFromCenterpoint > this.radius) ? distanceFromCenterpoint - this.radius:0;
return finalDistance;
}
}
});
Expand All @@ -107,7 +113,8 @@ When creating instance of the `Circle` you will actually call **TWO** different
```javascript
// TWO different initializers will be executed here!!!
const circle = Circle({ x: 12, y: 42, radius: 1.5 });
circle.distance({ x: 12, y: 42 }) === 0.5;
circle.distance({ x: 14, y: 42 }) === 0.5;
// x had to be 14 here for the math to check out.
```

Now, declaring couple of additional stamps. We'll use them to enrich JavaScript drawable objects.
Expand Down