#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <graphics.h>
#include <time.h>
#define MAX 100
int min = 0, max = 0, high, n = 40, UpOrDown, flag, frequency = 0, flag1 = 0, A1, B1;
double k, k1, b, d, highest, lowest;
struct {
int x;
int y;
}point[MAX];
void init() {
srand((unsigned)time(NULL));
for (int i = 0; i < n; i++) {
point[i].x = rand() % 1185 + 1;
point[i].y = rand() % 585 + 1;
circle(point[i].x, point[i].y, 3);
}
}
void jiefangcheng() {
k = (double)(point[min].y - point[max].y) / (double)(point[min].x - point[max].x);
b = (double)point[min].y - k * (double)point[min].x;
}
void findHigh() {
jiefangcheng();
flag = 0;
highest = 0;
lowest = 0;
for (int i = 0; i < n; i++) {
if (!((point[min].y == point[i].y && point[min].x == point[i].x) || (point[max].y == point[i].y && point[max].x == point[i].x))) {
d = fabs((double)(k * (double)point[i].x - (double)point[i].y + b) / sqrt(k * k + 1));
if (UpOrDown == 1) {
if ((k * point[i].x - point[i].y + b) > 0) {
flag = 1;
if (d > highest) {
highest = d;
high = i;
}
}
}
else {
if ((k * point[i].x - point[i].y + b) < 0) {
flag = 1;
if (d > lowest) {
lowest = d;
high = i;
}
}
}
}
}
}
void findMinAndMax() {
for (int i = 0; i < n; i++) {
if (point[i].x < point[min].x) min = i;
if (point[i].x > point[max].x) max = i;
}
}
void tuBao() {
frequency++;
setlinecolor(WHITE);
line(point[min].x, point[min].y, point[max].x, point[max].y);
Sleep(400);
findHigh();
if (flag == 0) {
if (frequency == 1) flag1 = 1;
return;
}
else {
if (!(point[min].x == point[A1].x && point[min].y == point[A1].y && point[max].x == point[B1].x && point[max].y == point[B1].y && flag1 == 1)) {
setlinecolor(BLACK);
line(point[min].x, point[min].y, point[max].x, point[max].y);
for (int i = 0; i < n; i++) {
setlinecolor(WHITE);
circle(point[i].x, point[i].y, 3);
}
Sleep(400);
}
}
int A = min;
int B = high;
int C = max;
min = A;
max = B;
printf("进入1\n");
tuBao();
min = B;
max = C;
printf("进入2\n");
tuBao();
}
int main() {
initgraph(1200, 600, SHOWCONSOLE);
init();
findMinAndMax();
A1 = min;
B1 = max;
UpOrDown = 1;
tuBao();
UpOrDown = 0;
min = A1;
max = B1;
tuBao();
while (1);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <graphics.h>
#include <time.h>
#define MAX 100
int min = 0, max = 0, high, n = 10, UpOrDown, flag, frequency = 0, flag1 = 0, A1, B1;
double k, k1, b, d, highest, lowest;
struct {
int x;
int y;
}point[MAX];
void init() {
srand((unsigned)time(NULL));
point[0].x = 0; point[0].y = 0;
point[1].x = -30; point[1].y = -40;
point[2].x = -30; point[2].y = -50;
point[3].x = -10; point[3].y = -60;
point[4].x = 50; point[4].y = -60;
point[5].x = 70; point[5].y = -50;
point[6].x = 90; point[6].y = -20;
point[7].x = 90; point[7].y = 10;
point[8].x = 80; point[8].y = 20;
point[9].x = 60; point[9].y = 30;
for (int i = 0; i < n; i++) {
point[i].x += 500;
point[i].y += 300;
circle(point[i].x, point[i].y, 3);
}
}
void jiefangcheng() {
k = (double)(point[min].y - point[max].y) / (double)(point[min].x - point[max].x);
b = (double)point[min].y - k * (double)point[min].x;
}
void findHigh() {
jiefangcheng();
flag = 0;
highest = 0;
lowest = 0;
for (int i = 0; i < n; i++) {
if (!((point[min].y == point[i].y && point[min].x == point[i].x) || (point[max].y == point[i].y && point[max].x == point[i].x))) {
d = fabs((double)(k * (double)point[i].x - (double)point[i].y + b) / sqrt(k * k + 1));
if (UpOrDown == 1) {
if ((k * point[i].x - point[i].y + b) > 0) {
flag = 1;
if (d > highest) {
highest = d;
high = i;
}
}
}
else {
if ((k * point[i].x - point[i].y + b) < 0) {
flag = 1;
if (d > lowest) {
lowest = d;
high = i;
}
}
}
}
}
}
void findMinAndMax() {
for (int i = 0; i < n; i++) {
if (point[i].x < point[min].x) min = i;
if (point[i].x > point[max].x) max = i;
}
}
void tuBao() {
frequency++;
setlinecolor(WHITE);
line(point[min].x, point[min].y, point[max].x, point[max].y);
Sleep(400);
findHigh();
if (flag == 0) {
if (frequency == 1) flag1 = 1;
return;
}
else {
if (!(point[min].x == point[A1].x && point[min].y == point[A1].y && point[max].x == point[B1].x && point[max].y == point[B1].y && flag1 == 1)) {
setlinecolor(BLACK);
line(point[min].x, point[min].y, point[max].x, point[max].y);
for (int i = 0; i < n; i++) {
setlinecolor(WHITE);
circle(point[i].x, point[i].y, 3);
}
Sleep(400);
}
}
int A = min;
int B = high;
int C = max;
min = A;
max = B;
printf("进入1\n");
tuBao();
min = B;
max = C;
printf("进入2\n");
tuBao();
}
int main() {
initgraph(1200, 600, SHOWCONSOLE);
init();
findMinAndMax();
A1 = min;
B1 = max;
UpOrDown = 1;
tuBao();
UpOrDown = 0;
min = A1;
max = B1;
tuBao();
while (1);
return 0;
}