#include <stdio.h>
// bool数据类型是C99标准新增的, 要使用bool类型需要导入<stdbool.h>头文件
#include <stdbool.h>
typedef struct point {
int x;
int y;
} LOPoint;
typedef struct size {
int width;
int height;
} LOSize;
typedef struct rect {
LOPoint origin;
LOSize size;
} LORect;
LOPoint LOPointMake(int x, int y);
LOPoint LOPointZero();
void printPointInfo(LOPoint pt);
LOSize LOSizeMake(int width, int height);
LOSize LOSizeZero();
void printSizeInfo(LOSize sz);
LORect LORectZero();
LORect LORectMake(int x, int y, int width, int height);
void printRectInfo(LORect rc);
int LORectGetMinX(LORect rect);
int LORectGetMaxX(LORect rect);
int LORectGetMinY(LORect rect);
int LORectGetMaxY(LORect rect);
LOPoint center(LORect rect);
bool LOPointEqualToPoint(LOPoint pt1, LOPoint pt2);
bool LOSizeEqualToSize(LOSize sz1, LOSize sz2);
bool LORectEqualToRect(LORect rect1, LORect rect2);
bool LORectContainsPoint(LORect rect, LOPoint pt);
bool LORectContainsRect(LORect rect1, LORect rect2);
bool LORectIsEmpty(LORect rect);
LORect LORectIntersection(LORect rect1, LORect rect2);
bool LORectIntersectsRect(LORect rect1, LORect rect2);
LORect LORectOffset(LORect rect, int dx, int dy);
LORect LORectInset(LORect rect, int dx, int dy);
void drawRect(LORect rect);
void drawEmptyRect(LORect rect);
// bool数据类型是C99标准新增的, 要使用bool类型需要导入<stdbool.h>头文件
#include <stdbool.h>
typedef struct point {
int x;
int y;
} LOPoint;
typedef struct size {
int width;
int height;
} LOSize;
typedef struct rect {
LOPoint origin;
LOSize size;
} LORect;
LOPoint LOPointMake(int x, int y);
LOPoint LOPointZero();
void printPointInfo(LOPoint pt);
LOSize LOSizeMake(int width, int height);
LOSize LOSizeZero();
void printSizeInfo(LOSize sz);
LORect LORectZero();
LORect LORectMake(int x, int y, int width, int height);
void printRectInfo(LORect rc);
int LORectGetMinX(LORect rect);
int LORectGetMaxX(LORect rect);
int LORectGetMinY(LORect rect);
int LORectGetMaxY(LORect rect);
LOPoint center(LORect rect);
bool LOPointEqualToPoint(LOPoint pt1, LOPoint pt2);
bool LOSizeEqualToSize(LOSize sz1, LOSize sz2);
bool LORectEqualToRect(LORect rect1, LORect rect2);
bool LORectContainsPoint(LORect rect, LOPoint pt);
bool LORectContainsRect(LORect rect1, LORect rect2);
bool LORectIsEmpty(LORect rect);
LORect LORectIntersection(LORect rect1, LORect rect2);
bool LORectIntersectsRect(LORect rect1, LORect rect2);
LORect LORectOffset(LORect rect, int dx, int dy);
LORect LORectInset(LORect rect, int dx, int dy);
void drawRect(LORect rect);
void drawEmptyRect(LORect rect);
函数定义:
#include "rect.h"
#include <stdlib.h>
LOPoint LOPointMake(int x, int y){
LOPoint pt;
pt.x = x;
pt.y = y;
return pt;
}
LOPoint LOPointZero(){
LOPoint pt;
pt.x = 0;
pt.y = 0;
return pt;
}
void printPointInfo(LOPoint pt){
printf("{%d, %d}\n", pt.x, pt.y);
}
LOSize LOSizeMake(int width, int height){
LOSize sz;
sz.width = width;
sz.height = height;
return sz;
}
LOSize LOSizeZero(){
LOSize sz;
sz.width = 0;
sz.height = 0;
return sz;
}
void printSizeInfo(LOSize sz){
printf("{%d, %d}\n", sz.width, sz.height);
}
LORect LORectZero(){
LORect rc;
rc.origin.x = 0;
rc.origin.y = 0;
rc.size.width = 0;
rc.size.height = 0;
return rc;
}
LORect LORectMake(int x, int y, int width, int height){
LORect rc;
rc.origin.x = x;
rc.origin.y = y;
rc.size.width = width;
rc.size.height = height;
return rc;
}
void printRectInfo(LORect rc){
printf("{{%d, %d}, {%d, %d}}\n", rc.origin.x, rc.origin.y, rc.size.width, rc.size.height);
}
int LORectGetMinX(LORect rect){
int LORectMinX = rect.origin.x;
return LORectMinX;
}
int LORectGetMaxX(LORect rect){
int LORectMaxX = rect.origin.x + rect.size.width;
return LORectMaxX;
}
int LORectGetMinY(LORect rect){
int LORectMinY = rect.origin.y;
return LORectMinY;
}
int LORectGetMaxY(LORect rect){
int LORectMaxY = rect.origin.y + rect.size.height;
return LORectMaxY;
}
LOPoint center(LORect rect){
LOPoint Center;
Center.x = rect.origin.x + rect.size.width / 2;
Center.y = rect.origin.y + rect.size.height / 2;
return Center;
}
bool LOPointEqualToPoint(LOPoint pt1, LOPoint pt2){
if (pt1.x == pt2.x && pt1.y == pt2.y) {
return 1;// 表示点相同
}else{
return 0;
}
}
bool LOSizeEqualToSize(LOSize sz1, LOSize sz2){
if (sz1.height == sz2.height && sz1.width == sz2.width) {
return 1;// 表示面积相同
}else{
return 0;
}
#include <stdlib.h>
LOPoint LOPointMake(int x, int y){
LOPoint pt;
pt.x = x;
pt.y = y;
return pt;
}
LOPoint LOPointZero(){
LOPoint pt;
pt.x = 0;
pt.y = 0;
return pt;
}
void printPointInfo(LOPoint pt){
printf("{%d, %d}\n", pt.x, pt.y);
}
LOSize LOSizeMake(int width, int height){
LOSize sz;
sz.width = width;
sz.height = height;
return sz;
}
LOSize LOSizeZero(){
LOSize sz;
sz.width = 0;
sz.height = 0;
return sz;
}
void printSizeInfo(LOSize sz){
printf("{%d, %d}\n", sz.width, sz.height);
}
LORect LORectZero(){
LORect rc;
rc.origin.x = 0;
rc.origin.y = 0;
rc.size.width = 0;
rc.size.height = 0;
return rc;
}
LORect LORectMake(int x, int y, int width, int height){
LORect rc;
rc.origin.x = x;
rc.origin.y = y;
rc.size.width = width;
rc.size.height = height;
return rc;
}
void printRectInfo(LORect rc){
printf("{{%d, %d}, {%d, %d}}\n", rc.origin.x, rc.origin.y, rc.size.width, rc.size.height);
}
int LORectGetMinX(LORect rect){
int LORectMinX = rect.origin.x;
return LORectMinX;
}
int LORectGetMaxX(LORect rect){
int LORectMaxX = rect.origin.x + rect.size.width;
return LORectMaxX;
}
int LORectGetMinY(LORect rect){
int LORectMinY = rect.origin.y;
return LORectMinY;
}
int LORectGetMaxY(LORect rect){
int LORectMaxY = rect.origin.y + rect.size.height;
return LORectMaxY;
}
LOPoint center(LORect rect){
LOPoint Center;
Center.x = rect.origin.x + rect.size.width / 2;
Center.y = rect.origin.y + rect.size.height / 2;
return Center;
}
bool LOPointEqualToPoint(LOPoint pt1, LOPoint pt2){
if (pt1.x == pt2.x && pt1.y == pt2.y) {
return 1;// 表示点相同
}else{
return 0;
}
}
bool LOSizeEqualToSize(LOSize sz1, LOSize sz2){
if (sz1.height == sz2.height && sz1.width == sz2.width) {
return 1;// 表示面积相同
}else{
return 0;
}
}
bool LORectEqualToRect(LORect rect1, LORect rect2){
if ((LOPointEqualToPoint(rect1.origin, rect2.origin)) && (LOSizeEqualToSize(rect1.size, rect2.size))) {
return 1;// 表示是同一个矩形
}else{
return 0;
}
}
bool LORectContainsPoint(LORect rect, LOPoint pt){
if (pt.x >= rect.origin.x && pt.y >= rect.origin.y && pt.x <= rect.origin.x + rect.size.width && pt.y <= rect.origin.y + rect.size.height) {
return 1;//包含点
}else{
return 0;
}
}
bool LORectContainsRect(LORect rect1, LORect rect2){
if (LORectGetMaxX(rect1) >= LORectGetMaxX(rect2) && LORectGetMaxY(rect1) >= LORectGetMaxY(rect2) && LORectGetMinX(rect1) <= LORectGetMinX(rect2) && LORectGetMinY(rect1) <= LORectGetMinY(rect2)) {
return 1;//rect1 包含 rect2
}else{
return 0;
}
}
bool LORectIsEmpty(LORect rect){
if (rect.size.height == 0 || rect.size.width == 0) {
return 1;// 面积为0
}else{
return 0;
}
}
LORect LORectIntersection(LORect rect1, LORect rect2){
LORect rect;
rect.origin.x = rect1.origin.x > rect2.origin.x ? rect1.origin.x : rect2.origin.x;
rect.origin.y = rect1.origin.y > rect2.origin.y ? rect1.origin.y : rect2.origin.y;
rect.size.width = ((rect1.origin.x + rect1.size.width) < (rect2.origin.x + rect2.size.width) ? (rect1.origin.x + rect1.size.width) : (rect2.origin.x + rect2.size.width)) - rect.origin.x;
rect.size.height = ((rect1.origin.y + rect1.size.height) < (rect2.origin.y + rect2.size.height) ? (rect1.origin.y + rect1.size.height) : (rect2.origin.y + rect2.size.height)) - rect.origin.y;
// if ((rect.origin.x + rect.size.width) > rect.origin.x && (rect.origin.y + rect.size.height) > rect.origin.y) {
return rect;
// }else {
// return LORectZero();
// }
}
bool LORectIntersectsRect(LORect rect1, LORect rect2){
LORect rect;
rect.origin.x = rect1.origin.x > rect2.origin.x ? rect1.origin.x : rect2.origin.x;
rect.origin.y = rect1.origin.y > rect2.origin.y ? rect1.origin.y : rect2.origin.y;
rect.size.width = ((rect1.origin.x + rect1.size.width) < (rect2.origin.x + rect2.size.width) ? (rect1.origin.x + rect1.size.width) : (rect2.origin.x + rect2.size.width)) - rect.origin.x;
rect.size.height = ((rect1.origin.y + rect1.size.height) < (rect2.origin.y + rect2.size.height) ? (rect1.origin.y + rect1.size.height) : (rect2.origin.y + rect2.size.height)) - rect.origin.y;
if (rect.size.width == 0 || rect.size.height == 0) {
return 0;// 不碰撞
}else{
return 1;//碰撞
}
}
LORect LORectOffset(LORect rect, int dx, int dy){
LORect rcos;
rcos.origin.x = rect.origin.x + dx;
rcos.origin.y = rect.origin.y + dy;
rcos.size.height = rect.size.height;
rcos.size.width = rect.size.width;
return rcos;
}
LORect LORectInset(LORect rect, int dx, int dy){
LORect rcls;
rcls.origin.x = rect.origin.x + dx;
rcls.origin.y = rect.origin.y + dy;
rcls.size.width = rect.size.width + 2 * abs(dx);
rcls.size.height = rect.size.height + 2 * abs(dy);
return rcls;
}
void drawRect(LORect rect){
for (int i = 0; i < LORectGetMinY(rect); i++) {
printf("\n");
}
int k = LORectGetMinY(rect);
while (k < LORectGetMaxY(rect)) {
for (int i = 0; i < LORectGetMinX(rect); i++) {
printf(" ");
}
for (int j = LORectGetMinX(rect); j < LORectGetMaxX(rect); j++) {
printf("*");
}
printf("\n");
k++;
}
}
void drawEmptyRect(LORect rect){
for (int i = 0; i < LORectGetMinY(rect); i++) {
printf("\n");
}
int k = LORectGetMinY(rect);
while (k < LORectGetMaxY(rect)) {
for (int i = 0; i < LORectGetMinX(rect); i++) {
printf(" ");
}
if ((k == LORectGetMaxY(rect) - 1) || k == LORectGetMinY(rect)) {
for (int j = LORectGetMinX(rect); j < LORectGetMaxX(rect); j++) {
printf("*");
}
}
else{
for (int j = LORectGetMinX(rect); j < LORectGetMaxX(rect); j++) {
if (j == LORectGetMinX(rect) || j == (LORectGetMaxX(rect) - 1)) {
printf("*");
}else{
printf(" ");
}
}
}
printf("\n");
k++;
}
}
if ((LOPointEqualToPoint(rect1.origin, rect2.origin)) && (LOSizeEqualToSize(rect1.size, rect2.size))) {
return 1;// 表示是同一个矩形
}else{
return 0;
}
}
bool LORectContainsPoint(LORect rect, LOPoint pt){
if (pt.x >= rect.origin.x && pt.y >= rect.origin.y && pt.x <= rect.origin.x + rect.size.width && pt.y <= rect.origin.y + rect.size.height) {
return 1;//包含点
}else{
return 0;
}
}
bool LORectContainsRect(LORect rect1, LORect rect2){
if (LORectGetMaxX(rect1) >= LORectGetMaxX(rect2) && LORectGetMaxY(rect1) >= LORectGetMaxY(rect2) && LORectGetMinX(rect1) <= LORectGetMinX(rect2) && LORectGetMinY(rect1) <= LORectGetMinY(rect2)) {
return 1;//rect1 包含 rect2
}else{
return 0;
}
}
bool LORectIsEmpty(LORect rect){
if (rect.size.height == 0 || rect.size.width == 0) {
return 1;// 面积为0
}else{
return 0;
}
}
LORect LORectIntersection(LORect rect1, LORect rect2){
LORect rect;
rect.origin.x = rect1.origin.x > rect2.origin.x ? rect1.origin.x : rect2.origin.x;
rect.origin.y = rect1.origin.y > rect2.origin.y ? rect1.origin.y : rect2.origin.y;
rect.size.width = ((rect1.origin.x + rect1.size.width) < (rect2.origin.x + rect2.size.width) ? (rect1.origin.x + rect1.size.width) : (rect2.origin.x + rect2.size.width)) - rect.origin.x;
rect.size.height = ((rect1.origin.y + rect1.size.height) < (rect2.origin.y + rect2.size.height) ? (rect1.origin.y + rect1.size.height) : (rect2.origin.y + rect2.size.height)) - rect.origin.y;
// if ((rect.origin.x + rect.size.width) > rect.origin.x && (rect.origin.y + rect.size.height) > rect.origin.y) {
return rect;
// }else {
// return LORectZero();
// }
}
bool LORectIntersectsRect(LORect rect1, LORect rect2){
LORect rect;
rect.origin.x = rect1.origin.x > rect2.origin.x ? rect1.origin.x : rect2.origin.x;
rect.origin.y = rect1.origin.y > rect2.origin.y ? rect1.origin.y : rect2.origin.y;
rect.size.width = ((rect1.origin.x + rect1.size.width) < (rect2.origin.x + rect2.size.width) ? (rect1.origin.x + rect1.size.width) : (rect2.origin.x + rect2.size.width)) - rect.origin.x;
rect.size.height = ((rect1.origin.y + rect1.size.height) < (rect2.origin.y + rect2.size.height) ? (rect1.origin.y + rect1.size.height) : (rect2.origin.y + rect2.size.height)) - rect.origin.y;
if (rect.size.width == 0 || rect.size.height == 0) {
return 0;// 不碰撞
}else{
return 1;//碰撞
}
}
LORect LORectOffset(LORect rect, int dx, int dy){
LORect rcos;
rcos.origin.x = rect.origin.x + dx;
rcos.origin.y = rect.origin.y + dy;
rcos.size.height = rect.size.height;
rcos.size.width = rect.size.width;
return rcos;
}
LORect LORectInset(LORect rect, int dx, int dy){
LORect rcls;
rcls.origin.x = rect.origin.x + dx;
rcls.origin.y = rect.origin.y + dy;
rcls.size.width = rect.size.width + 2 * abs(dx);
rcls.size.height = rect.size.height + 2 * abs(dy);
return rcls;
}
void drawRect(LORect rect){
for (int i = 0; i < LORectGetMinY(rect); i++) {
printf("\n");
}
int k = LORectGetMinY(rect);
while (k < LORectGetMaxY(rect)) {
for (int i = 0; i < LORectGetMinX(rect); i++) {
printf(" ");
}
for (int j = LORectGetMinX(rect); j < LORectGetMaxX(rect); j++) {
printf("*");
}
printf("\n");
k++;
}
}
void drawEmptyRect(LORect rect){
for (int i = 0; i < LORectGetMinY(rect); i++) {
printf("\n");
}
int k = LORectGetMinY(rect);
while (k < LORectGetMaxY(rect)) {
for (int i = 0; i < LORectGetMinX(rect); i++) {
printf(" ");
}
if ((k == LORectGetMaxY(rect) - 1) || k == LORectGetMinY(rect)) {
for (int j = LORectGetMinX(rect); j < LORectGetMaxX(rect); j++) {
printf("*");
}
}
else{
for (int j = LORectGetMinX(rect); j < LORectGetMaxX(rect); j++) {
if (j == LORectGetMinX(rect) || j == (LORectGetMaxX(rect) - 1)) {
printf("*");
}else{
printf(" ");
}
}
}
printf("\n");
k++;
}
}