UVA_10902_Pick-up Sticks

#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
#include<numeric>
#pragma warning(disable:4996)
using std::cin;
using std::cout;
using std::endl;
using std::stringstream;
using std::string;
using std::vector;
using std::list;
using std::pair;
using std::set;
using std::multiset;
using std::map;
using std::multimap;
using std::stack;
using std::queue;
using std::priority_queue;
class Point
{
public:
	double x, y;
	Point() {}
	Point(const double &X, const double &Y)
	{
		x = X;
		y = Y;
	}
};
class Line
{
public:
	double a, b, c;//ax+by=c
	double x_min, x_max, y_min, y_max;//直线的值域
	Line() {}
	Line(const Point &first, const Point &second)
	{
		x_min = std::min(first.x, second.x);
		x_max = std::max(first.x, second.x);
		y_min = std::min(first.y, second.y);
		y_max = std::max(first.y, second.y);
		if (first.x != second.x)//斜率式可行
		{
			b = 1;
			a = -(first.y - second.y) / (first.x - second.x);
			c = first.y + a*first.x;
		}
		else//k->无穷
		{
			b = 0;
			a = 1;
			c = first.x;
		}
	}
	bool lineIntersected(const Line &line)//直线相交的判定
	{
		auto D = a*line.b - line.a*b;
		if (D)
		{
			return true;
		}
		return false;
	}
	bool lineParallel(const Line&line)//判断两直线是否平行
	{
		auto D = a*line.b - line.a*b;
		if (!D)
		{
			return true;
		}
		return false;
	}
	bool lineOverlapped(const Line&line)//判断两直线是否重合(平行的特例)
	{
		auto D = a*line.b - line.a*b;
		auto Dx = c*line.b - line.c*b;
		auto Dy = a*line.c - line.a*c;
		if (!D&&!Dx&&!Dy)
		{
			return true;
		}
		return false;
	}
	Point getIntersection(const Line&line)//行列式求两直线交点
	{
		auto D = a*line.b - line.a*b;
		auto Dx = c*line.b - line.c*b;
		auto Dy = a*line.c - line.a*c;
		return{ Dx / D,Dy / D };
	}
	bool segmentIntersected(const Line &line)
	{
		if (lineIntersected(line))
		{
			auto point = getIntersection(line);
			if (point.x >= x_min&&point.x <= x_max
				&&point.y >= y_min&&point.y <= y_max
				&&point.x >= line.x_min&&point.x <= line.x_max
				&&point.y >= line.y_min&&point.y <= line.y_max
				)//交点在两线段的值域内
			{
				return true;
			}
		}
		return false;
	}
	bool segmentOverlapped(const Line &line)
	{
		if (lineOverlapped(line))
		{
			if (x_min <= line.x_max || x_max >= line.x_min)
			{
				return true;
			}
		}
		return false;
	}
};
class Rectangle
{
public:
	int left, right, top, bottom;
	bool inRectangle(const Point &point)
	{
		if (point.x >= left&&point.x <= right&&point.y >= bottom&&point.y <= top)
		{
			return true;
		}
		return false;
	}
};
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n;
	while (cin >> n&&n)
	{
		vector<Line>line(1);
		for (int i = 1; i <= n; i++)
		{
			double x1, x2, y1, y2;
			cin >> x1 >> y1 >> x2 >> y2;
			line.push_back({ { x1,y1 },{ x2,y2 } });
		}
		vector<bool>overlapped(n + 1);
		for (int i = 1; i <= n; i++)
		{
			for (int j = i+1; j <=n; j++)
			{
				if (line[i].segmentIntersected(line[j]) || line[i].segmentOverlapped(line[j]))
				{
					overlapped[i] = true;
					break;
				}
			}
		}
		cout << "Top sticks:";
		bool flag = false;
		for (int i = 1; i <= n; i++)
		{
			if (!overlapped[i])
			{
				if (flag)
				{
					cout << ',';
				}
				flag = true;
				cout << ' ' << i;
			}
		}
		cout << '.' << endl;
	}
	return 0;
}
#include<iostream>    
#include<sstream>    
#include<string>    
#include<vector>    
#include<list>    
#include<set>    
#include<map>  
#include<stack>    
#include<queue>    
#include<algorithm>    
#include<numeric>    
#include<cmath>  
#include<cstdio>  
#include<cstdlib>  
#include<cstring>  
#pragma warning(disable:4996) 
using namespace std;
const double eps = 1e-8;
const double pi = acos(-1.0);
int cmp(const double &x)
{
	if (fabs(x) < eps)
	{
		return 0;
	}
	if (x > 0)
	{
		return 1;
	}
	return -1;
}
inline double sqr(const double &x)
{
	return x*x;
}
class Point
{
public:
	double x, y;
	Point() {}
	Point(const double &a, const double &b) :x(a), y(b) {}
	void input()
	{
		cin >> x >> y;
	}
	friend Point operator+(const Point &a, const Point &b)
	{
		return Point(a.x+b.x,a.y+b.y);
	}
	friend Point operator-(const Point &a, const Point &b)
	{
		return Point(a.x - b.x, a.y - b.y);
	}
	friend bool operator==(const Point &a, const Point &b)
	{
		return cmp(a.x - b.x) == 0 && cmp(a.y - b.y) == 0;
	}
	friend Point operator*(const Point &a,const double &b)
	{
		return Point(a.x*b, a.y*b);
	}
	friend Point operator*(const double &a, const Point &b)
	{
		return Point(b.x*a, b.y*a);
	}
	friend Point operator/(const Point &a, const double &b)
	{
		return Point(a.x / b, a.y / b);
	}
	double norm()
	{
		return sqrt(sqr(x)+ sqr(y));
	}
};
double det(const Point &a, const Point &b)
{
	return a.x*b.y - a.y * b.x;
}
double dot(const Point &a, const Point &b)
{
	return a.x*b.x + a.y * b.y;
}
double dist(const Point &a, const Point &b)
{
	return (a - b).norm();
}
Point rotate_point(const Point&p,const double &A)
{
	double tx = p.x, ty = p.y;
	return Point(tx*cos(A)-ty*sin(A),tx*sin(A)+ty*cos(A));
}

class Line
{
public:
	Point a, b;
	Line() {}
	Line(const Point&x, const Point &y) :a(x), b(y) {}
	Line point_make_line(const Point &a,const Point &b)
	{
		return Line(a, b);
	}
};
double dist_point_segment(const Point &p,const Point &s,const Point &t)
{
	if (cmp(dot(p-s,t-s))<0)
	{
		return (p - s).norm();
	}
	if (cmp(dot(p -t , s - t)) < 0)
	{
		return (p - t).norm();
	}
	return fabs(det(s - p, t - p) / dist(s, t));
}
void PointProjLine(const Point &p,const Point &s,const Point&t,Point &cp)
{
	double r = dot((t-s),(p-s))/dot(t-s,t-s);
	cp = s + r*(t - s);
}
bool PointOnSegment(const Point&p,const Point &s,const Point &t)
{
	return cmp(det(p - s, t - s)) == 0 && cmp(dot(p - s, p - t)) <= 0;
}
bool parallel(const Line&a,const Line&b)
{
	return !cmp(det(a.a - a.b, b.a - b.b));
}
bool line_make_point(const Line &a, const Line&b, Point &res)
{
	if (parallel(a, b)) return false;
	double s1 = det(a.a - b.a, b.b - b.a);
	double s2 = det(a.b - b.a, b.b - b.a);
	res = (s1*a.b - s2*a.a) / (s1 - s2);
	return true;
}
Line move_d(const Line &a, const double &len)
{
	Point d = a.b - a.a;
	d = d/d.norm();
	d = rotate_point(d, pi / 2);
	return Line(a.a + d*len, a.b + d*len);
}
int main()
{
	//freopen("input.txt", "r", stdin);    
	//freopen("output.txt", "w", stdout);   
	int n;
	while (cin >> n&&n)
	{
		vector<Line>line(n);
		for (int i = 0; i < n; i++)
		{
			cin >> line[i].a.x >> line[i].a.y >> line[i].b.x >> line[i].b.y;
		}
		vector<bool>overlapped(n);
		for (int i = 0; i < n; i++)
		{
			for (int j = i + 1; j < n; j++)
			{
				Point point;
				if (line_make_point(line[i],line[j],point)&&PointOnSegment(point,line[i].a,line[i].b)&&PointOnSegment(point,line[j].a,line[j].b))
				{
					overlapped[i] = true;
					break;
				}
			}
		}
		cout << "Top sticks:";
		bool flag = false;
		for (int i = 0; i < n; i++)
		{
			if (!overlapped[i])
			{
				if (flag)
				{
					cout << ',';
				}
				flag = true;
				cout << ' ' << i+1;
			}
		}
		cout << '.' << endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`pygame.time.get_ticks()` 是 Pygame 中的一个函数,用于获取从 Pygame 初始化到当前时刻的毫秒数。你可以使用这个函数来实现游戏中的定时器,或者测量游戏中的一些操作所需的时间。 以下是一个简单的示例,演示如何使用 `pygame.time.get_ticks()` 函数来实现一个简单的定时器: ``` import pygame pygame.init() # 设置窗口大小和标题 size = (400, 300) screen = pygame.display.set_mode(size) pygame.display.set_caption("定时器示例") # 设置字体 font = pygame.font.Font(None, 36) # 定义变量 start_ticks = pygame.time.get_ticks() elapsed_time = 0 # 游戏循环 done = False while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True # 计算经过的时间 elapsed_time = pygame.time.get_ticks() - start_ticks # 在屏幕上绘制经过的时间 text = font.render("经过的时间: " + str(elapsed_time) + " 毫秒", True, (255, 255, 255)) screen.blit(text, (50, 50)) # 更新屏幕 pygame.display.flip() # 控制帧速率 clock = pygame.time.Clock() clock.tick(60) # 退出 Pygame pygame.quit() ``` 在上面的示例中,我们首先调用 `pygame.time.get_ticks()` 函数获取当前的毫秒数,并将其存储在 `start_ticks` 变量中。然后,在游戏循环中,我们计算经过的时间(即当前的毫秒数减去 `start_ticks` 变量),并将其存储在 `elapsed_time` 变量中。最后,我们使用 `pygame.font.Font()` 函数设置字体,并使用 `pygame.Surface.blit()` 函数在屏幕上绘制文本。注意,我们还使用 `pygame.time.Clock()` 类来控制游戏的帧速率,以确保游戏运行流畅。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值