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

In chapter 16, exercise 10b: The midpoint formula is required not the average length of the difference #136

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

MKing8992
Copy link

@MKing8992 MKing8992 commented Dec 11, 2024

Corrected the midpoint calculation in the function by using the proper midpoint formula: [(x_2 + x_1)/2, (y_2 + y_1)/2], rather than mistakenly calculating half the distance between points.

Prompt for chapter 16 exercise 10(b):

Given:

struct point {int x, y;);
struct rectangle { struct point upper_left, lower_right; };

(b) Compute the center of r, returning it as point value. If either the x or y coordinate of the center isn't an integer, store its truncated value in the point structure

The usage of [(x_2 - x_1)/2, (y_2 - y_1)/2] in the current solution below would not be the correct [x,y] coordinates

struct point rectangle_center(struct rectangle r) {
    return (struct point) {(r.lower_right.x - r.upper_left.x) / 2,
                           (r.lower_right.y - r.upper_left.y) / 2};
}

Corrected the midpoint calculation in the function by using the proper midpoint formula: [(x2 + x1)/2, (y2 + y1)/2], rather than mistakenly calculating half the distance between points.
@MKing8992 MKing8992 changed the title Fix incorrect math formula in rectangle_center() In chapter 16, exercise 10b: The midpoint formula is required not the average length of the difference Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant