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

Ch 16 ex 10(b). The formula for finding the center coordinates of (struct rectangle) r must be the midpoint formula NOT the average length between the coordinates #135

Open
MKing8992 opened this issue Dec 11, 2024 · 1 comment

Comments

@MKing8992
Copy link

MKing8992 commented Dec 11, 2024

Hello. Excited to contribute! This should be a really easy fix!
Prompt for chapter 16 exercise 10(b):

The following structures are designed to store information about the objects on a graphics screen:

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

Current solution:

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};
}

With the midpoint formula in mind the correct usage is:

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};
}
@MKing8992
Copy link
Author

Sorry. Still learning the ins and outs. I don't know if posting this was necessary or out. I have made a pull request for the problem mentioned here.

@MKing8992 MKing8992 changed the title The correct mathematical formula for finding the center coordinates of (struct rectangle) r must be the midpoint formula NOT the average length between the coordinates Ch 16 ex 10(b). The formula for finding the center coordinates of (struct rectangle) r must be the midpoint formula NOT the average length between the coordinates 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

No branches or pull requests

1 participant