text stringlengths 49 983k |
|---|
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (int)n; i++)
const double eps = 1e-10;
struct Point {
double x, y;
Point(double x = 0.0, double y = 0.0) : x(x), y(y) {}
double norm() {return sqrt(x*x + y*y);}
};
Point operator + (const Point &p, const Point &q) {return Point(p.x + q.x, p.y + q.y);}
Point operator - (const Point &p, const Point &q) {return Point(p.x - q.x, p.y - q.y);}
double operator * (const Point &p, const Point q) {return p.x * q.x + p.y * q.y;}
bool operator == (const Point &p, const Point &q) {return (abs(p.x - q.x) < eps && abs(p.y - q.y) < eps);}
bool operator != (const Point &p, const Point &q) {return !(p == q);}
double cross(const Point &p, const Point &q) {return p.x*q.y - p.y*q.x;}
double dist(const Point &p, const Point &q) {return (p-q).norm();}
struct Segment {
Point p , q;
Segment(Point p = Point(0.0,0.0), Point q = Point(0.0,0.0)) : p(p), q(q) {}
};
struct Line {
double a, b, c;
Line(double a, double b, double c) : a(a), b(b), c(c) {}
Line(Point p, Point q) {
a = q.y - p.y;
b = p.x - q.x;
c = q.x * p.y - p.x * q.y;
}
};
struct Circle {
double r;
Point p;
Circle(Point p = Point(0.0, 0.0), double r = 0.0) : p(p), r(r) {}
};
// 点の直線への射影
Point projection (Point p, Line l) {
double x = p.x - l.a * (l.a*p.x + l.b*p.y + l.c) / (l.a*l.a + l.b*l.b);
double y = p.y - l.b * (l.a*p.x + l.b*p.y + l.c) / (l.a*l.a + l.b*l.b);
return Point(x,y);
}
// 線対称な点
Point reflection(Point p, Line l) {
double x = p.x - 2.0 * l.a * (l.a*p.x + l.b*p.y + l.c) / (l.a*l.a + l.b*l.b);
double y = p.y - 2.0 * l.b * (l.a*p.x + l.b*p.y + l.c) / (l.a*l.a + l.b*l.b);
return Point(x,y);
}
// 点の線分からの回転方向
int ccw(Point p, Point p1, Point p2) {
p1 = p1 - p; p2 = p2 - p;
if(cross(p1,p2) > eps) return 1;
if(cross(p1,p2) < -eps) return -1;
if(p1 * p2 < 0) return 2;
if(p1.norm() < p2.norm()) return -2;
return 0;
}
// 点が線分上にあるか判定
bool on_segment(Point p, Segment s) {
return (ccw(s.p,s.q,p) == 0 ? true : false);
}
// 線分の交差判定
bool segment_segment_cross(const Segment l, const Segment s) {
return ccw(l.p,l.q,s.p) * ccw(l.p,l.q,s.q) <= 0 && ccw(s.p,s.q,l.p) * ccw(s.p,s.q,l.q) <= 0;
}
// 直線の平行・直交判定 (平行:2 直交:1 その他:0)
int line_parallel_orthogonal(Line l, Line s) {
if(abs(l.a*s.a + l.b*s.b) < eps) return 1;
else if(abs(l.a*s.b - l.b*s.a) < eps) return 2;
else return 0;
}
// 直線と直線の交点
vector<Point> line_line_cross(Line l, Line s) {
vector<Point> vp;
if(line_parallel_orthogonal(l,s) != 2) {
Point p;
p.x = (l.b*s.c - s.b*l.c) / (l.a*s.b - s.a*l.b);
p.y = (-l.a*s.c + s.a*l.c) / (l.a*s.b - s.a*l.b);
vp.push_back(p);
}
return vp;
}
// 点と直線の距離
double point_line_dist(Point p, Line l) {
return abs(l.a*p.x + l.b*p.y + l.c) / sqrt(l.a*l.a + l.b*l.b);
}
// 点と線分の距離
double point_segment_dist(Point p, Segment s) {
Point h = projection(p,Line(s.p,s.q));
if(on_segment(h,s)) return dist(p,h);
else return min(dist(p,s.p),dist(p,s.q));
}
// 線分と線分の距離
double segment_segment_dist(Segment l, Segment s) {
if(segment_segment_cross(l,s)) return 0.0;
else return min({point_segment_dist(l.p,s),point_segment_dist(l.q,s),point_segment_dist(s.p,l),point_segment_dist(s.q,l)});
}
// 直線と直線の距離
double line_line_dist(Line l, Line s) {
if(line_parallel_orthogonal(l,s) != 2) return 0.0;
else if(abs(s.a) < eps) return point_line_dist(Point(0.0,-s.c/s.b),l);
else return point_line_dist(Point(-s.c/s.a,0),l);
}
// 直線と線分の距離
double line_segment_dist(Line l, Segment s) {
Point a, b;
if(abs(l.a) < eps) a = Point(0.0,-l.c/l.b), b = Point(1.0,-l.c/l.b);
else if(abs(l.b) < eps) a = Point(-l.c/l.a,0.0), b = Point(-l.c/l.a,0.0);
else a = Point(0.0,-l.c/l.b), b = Point(-l.c/l.a,0);
if(ccw(a,b,s.p) * ccw(a,b,s.q) <= 0) return 0.0;
else return min(point_line_dist(s.p,l),point_line_dist(s.q,l));
}
// ★円と円の交差判定 (離れる:4 外接:3 交わる:2 内接:1 内包:0)
int circle_circle_pos(Circle c, Circle d) {
if(dist(c.p,d.p) >= c.r + d.r + eps) return 4;
else if(abs(dist(c.p,d.p) - c.r - d.r) < eps) return 3;
else if(dist(c.p,d.p) >= abs(c.r-d.r) + eps) return 2;
else if(abs(dist(c.p,d.p) - abs(c.r-d.r)) < eps) return 1;
else return 0;
}
// ★円と直線の交点
vector<Point> circle_line_cross(Circle c, Line l) {
vector<Point> vp;
Point h = projection(c.p,l);
double d = l.a*c.p.x + l.b*c.p.y + l.c;
double q = c.r*c.r - d*d / (l.a*l.a + l.b*l.b);
if(q >= eps) {
Point p1, p2;
p1.x = h.x + sqrt(q) * l.b / sqrt(l.a*l.a + l.b*l.b);
p1.y = h.y - sqrt(q) * l.a / sqrt(l.a*l.a + l.b*l.b);
p2.x = h.x - sqrt(q) * l.b / sqrt(l.a*l.a + l.b*l.b);
p2.y = h.y + sqrt(q) * l.a / sqrt(l.a*l.a + l.b*l.b);
vp.push_back(p1);
vp.push_back(p2);
} else if(abs(q) < eps) {
vp.push_back(h);
}
return vp;
}
// ★円と円の交点
vector<Point> circle_circle_cross(Circle c, Circle d) {
Line l(2.0*(c.p.x-d.p.x),2.0*(c.p.y-d.p.y),-c.p.x*c.p.x+d.p.x*d.p.x-c.p.y*c.p.y+d.p.y*d.p.y+c.r*c.r-d.r*d.r);
return circle_line_cross(c,l);
}
// ★多角形の面積
double polygon_area(vector<Point>& v) {
int l = v.size();
double s = 0.0;
for(int i = 0; i < l; i++) s += 1/2.0 * cross(v[i],v[(i+1)%l]);
return s;
}
// ★多角形の凸性
bool polygon_convex(vector<Point>& v) {
int l = v.size();
for(int i = 0; i < l; i++) {
if(cross(v[(i+1)%l]-v[i],v[(i+2)%l]-v[i]) < -eps) return false;
}
return true;
}
int main() {
int n;
cin >> n;
vector<Point> v(n);
rep(i,n) cin >> v[i].x >> v[i].y;
cout << (polygon_convex(v)? 1 : 0) << endl;
}
|
#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
#define pa pair<int,int>
const int N=2e5+10;
const int mod=998244353;
using namespace std;
struct point{ int x,y;}p[N];
inline ll cross(point a,point b,point c){return 1LL*(b.x-a.x)*(c.y-a.y)-1LL*(b.y-a.y)*(c.x-a.x); }
int main()
{
int n; scanf("%d",&n);
for(int i=0;i<n;i++) scanf("%d%d",&p[i].x,&p[i].y);
int ff=1;
for(int i=0;i<n;i++)
{
int j=(i+1)%n,k=(i+2)%n;
if(cross(p[i],p[j],p[k])<0) {ff=0; break;}
}
if(ff)puts("1");else puts("0");
return 0;
}
/*
*/
|
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-8;
const double INF = 1e12;
//point
typedef complex<double> P;
namespace std {
bool operator < (const P& a, const P& b) {
return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}
}
double cross(const P& a, const P& b) {
return imag(conj(a)*b);
}
double dot(const P& a, const P& b) {
return real(conj(a)*b);
}
int ccw(P a, P b, P c) {
b -= a; c -= a;
if (cross(b, c) > 0) return +1; // counter clockwise
if (cross(b, c) < 0) return -1; // clockwise
if (dot(b, c) < 0) return +2; // c--a--b on line
if (norm(b) < norm(c)) return -2; // a--b--c on line
return 0; // a--c--b on line
}
// line
struct L : public vector<P> {
L(const P& a, const P& b) {
push_back(a); push_back(b);
}
};
// polygon
typedef vector<P> G;
P extreme(const vector<P> &po, const L &l) {
int k = 0;
for (int i = 1; i < po.size(); ++i)
if (dot(po[i], l[1]-l[0]) > dot(po[k], l[1]-l[0])) k = i;
return po[k];
}
enum { OUT, ON, IN };
int contains(const G& po, const P& p) {
bool in = false;
for (int i = 0; i < po.size(); ++i) {
P a = po[i] - p, b = po[(i+1)%po.size()] - p;
if (imag(a) > imag(b)) swap(a, b);
if (imag(a) <= 0 && 0 < imag(b))
if (cross(a, b) < 0) in = !in;
if (cross(a, b) == 0 && dot(a, b) <= 0) return ON;
}
return in ? IN : OUT;
}
double area2(const G& po) {
double A = 0;
for (int i = 0; i < po.size(); ++i)
A += cross(po[i], po[(i+1)%po.size()]);
//????????????????????????????????¨????????????
return A/2;
}
bool isconvex(const G &p) {
int n = p.size();
if(cross(p[0]-p[n-1],p[n-2]-p[n-1]) < 0) return false;
for(int i = 1; i < n-1; ++i) {
if(cross(p[i+1]-p[i],p[i-1]-p[i]) < 0) return false;
}
return true;
}
int main() {
int n;
cin >> n;
G po;
for(int i=0; i<n; ++i) {
double x, y;
cin >> x >> y;
P p(x, y);
po.push_back(p);
}
if(isconvex(po)) cout << 1 << endl;
else cout << 0 << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define eps = (1e-10);
//点
struct Point{
double x,y;
Point(double _x=0,double _y=0):x(_x),y(_y){}
Point operator + (Point p){return Point(x+p.x,y+p.y);}
Point operator - (Point p){return Point(x-p.x,y-p.y);}
Point operator * (double a){return Point(a*x,a*y);}
Point operator / (double a){return Point(x/a,y/a);}
double norm(){return x*x+y*y;}//模的平方
double ABS() {return sqrt(norm());}//模
};
//线段
struct Segment{
Point p1,p2;
};
double dot(Point a,Point b){
return a.x*b.x+a.y*b.y;
}
double cross(Point a,Point b){
return a.x*b.y-a.y*b.x;
}
vector<Point> polygon;
bool check(vector<Point> polygon){
int n=(int)polygon.size();
polygon.push_back(polygon[0]);
polygon.push_back(polygon[2]);
for(int i=0;i<n;i++){
Point a=polygon[i+1]-polygon[i];
Point b=polygon[i+2]-polygon[i+1];
if(cross(a,b)<0) return 0;
}
return 1;
}
int main(){
int n;
scanf("%d",&n);
double x,y;
for(int i=1;i<=n;i++){
scanf("%lf %lf",&x,&y);
polygon.push_back({x,y});
}
if(check(polygon)) puts("1");
else puts("0");
return 0;
}
|
#include <iostream>
#include <cmath>
#include <complex>
#include <tuple>
#include <vector>
using namespace std;
using vec = complex<double>;
using line = pair<vec,vec>;
using polygon = vector<vec>;
const double eps = 0.0000001;
double inner_product(vec u,vec v){
return real(u*conj(v));
}
double cross_product(vec u,vec v){
return imag(conj(u)*v);
}
vec projection(line l,vec p){//p???l???????°???±
vec s=l.first, t=l.second;
double k = inner_product(t-s,p-s)/inner_product(t-s,t-s);
return (1.0-k)*s+k*t;
}
vec reflection(line l,vec p){
return 2.0*projection(l,p)-p;
}
int ccw(vec& a, vec& b, vec& c){
vec ab = b-a, ac = c-a;
double o = cross_product(ab,ac);
if(o>0) return 1; //CCW
if(o<0) return -1; //CW
if(inner_product(ab,ac)<0){
return 2; //C-A-B
}else{
if(inner_product(ab,ab)<inner_product(ac,ac)){
return -2; //A-B-C
}else{
return 0; //A-C-B
}
}
}
bool isIntersect(line l0, line l1){
int s = ccw(l0.first,l0.second,l1.first)*ccw(l0.first,l0.second,l1.second);
if(s!=0&&s!=-1&&s!=-4) return false;
s=ccw(l1.first,l1.second,l0.first)*ccw(l1.first,l1.second,l0.second);
if(s!=0&&s!=-1&&s!=-4) return false;
else return true;
}
vec interSection(line l0, line l1){
vec s0, t0, s1, t1;
tie(s0,t0)=l0; tie(s1,t1)=l1;
double k = cross_product(t1-s1,s1-s0)/cross_product(t1-s1,t0-s0);
return s0+(t0-s0)*k;
}
double segLength(line l){
return abs(l.first-l.second);
}
double distLine2point(line l, vec p){
return abs(cross_product(l.second-l.first,p-l.first))/abs(l.second-l.first);
}
double distSeg2Point(line l, vec p){
vec x = projection(l,p);
double L = segLength(l);
if(abs(x-l.first)<L&&abs(x-l.second)<L){
return abs(x-p);
}else{
return min(abs(l.first-p),abs(l.second-p));
}
}
double distSeg2Seg(line l1, line l2){
if(isIntersect(l1,l2)) return 0;
double ret = 1e20;
ret = min(ret,distSeg2Point(l1,l2.first));
ret = min(ret,distSeg2Point(l1,l2.second));
swap(l1,l2);
ret = min(ret,distSeg2Point(l1,l2.first));
ret = min(ret,distSeg2Point(l1,l2.second));
return ret;
}
double area(polygon& g){
double S=0;
for(auto i = g.begin()+1; i!=g.end();++i){
S += cross_product(*(i-1),*i);
}
S/=2;
return S;
}
bool isConvex(polygon& g){
int n=g.size()-2;
for(int i=0;i<n;i++){
if(!~ccw(g[i],g[i+1],g[i+2]))return false;
}
return true;
}
//???????????§???????????????
int main(){
int n;
cin >> n;
polygon g;
while(n--){
int x,y;
cin>>x>>y;
g.emplace_back(x,y);
}
g.push_back(g[0]);
g.push_back(g[1]);
cout.precision(1);
cout << fixed;
cout << (isConvex(g)?1:0) << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef double ll;
struct Point
{
ll x,y;
Point(){ x = y = 0.0; }
Point(ll _x, ll _y) : x(_x), y(_y){}
Point operator+ (Point p)
{
return Point(p.x + x, p.y + y);
}
Point operator- (Point p)
{
return Point(x - p.x, y - p.y);
}
ll operator* (Point p)
{
return p.x * x + p.y * y;
}
Point operator* (ll t)
{
return Point(x*t, y*t);
}
ll operator~ ()
{
return x * x + y * y;
}
ll operator% (Point p)
{
return x * p.y - y * p.x;
}
};
Point polygon[200];
int n;
bool ccw(Point a, Point b, Point p)
{
return ( (b-a)%(p-a) ) < 0;
}
bool isConvex()
{
if(n+1 <= 3) return false;
bool isLeft = ccw(polygon[0], polygon[1], polygon[2]);
for(int i = 1 ; i < n; i++)
if(ccw(polygon[i-1],polygon[i],polygon[i+1]) != isLeft)
return false;
return true;
}
int main()
{
cout << fixed;
cout.precision(1);
ios::sync_with_stdio(0); cin.tie(0);
Point p;
cin >> n;
for(int i = 0 ; i < n ; i++)
cin >> polygon[i].x >> polygon[i].y;
polygon[n] = polygon[0];
cout << isConvex() << '\n';
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define EPS 1e-10
struct Point{
double x,y;
Point(){}
Point(double x,double y) : x(x),y(y) {}
Point operator - (const Point &p)const{ return Point(x-p.x,y-p.y); }
};
double dot(const Point &a,const Point &b){ return a.x*b.x+a.y*b.y; }
double cross(const Point &a,const Point &b){ return a.x*b.y - b.x*a.y; }
double norm(const Point &p){ return dot(p,p); }
#define COUNTER_CLOCKWISE 1
#define CLOCKWISE -1
#define ONLINE_BACK 2
#define ONLINE_FRONT -2
#define ON_SEGMENT 0
typedef Point Vector;
int ccw(Point p0,Point p1,Point p2){
Vector a = p1 - p0;
Vector b = p2 - p0;
if(cross(a,b) > EPS) return COUNTER_CLOCKWISE;
if(cross(a,b) < -EPS) return CLOCKWISE;
if(dot(a,b) < -EPS) return ONLINE_BACK;
if(norm(a) < norm(b)) return ONLINE_FRONT;
return ON_SEGMENT;
}
typedef vector<Point> Polygon;
#define prev(G,i) (G[(i-1+G.size())%G.size()])
#define curr(G,i) (G[i%G.size()])
#define next(G,i) (G[(i+1)%G.size()])
bool isConvex(const Polygon &p){
for(int i = 0 ; i < (int)p.size() ; i++){
if(ccw(prev(p,i),curr(p,i),next(p,i)) == CLOCKWISE){
return false;
}
}
return true;
}
int main(){
int N;
cin >> N;
Polygon p(N);
for(int i = 0 ; i < N ; i++){
cin >> p[i].x >> p[i].y;
}
cout << isConvex(p) << endl;
return 0;
} |
#include <iostream>
#include <complex>
#include <utility>
#include <vector>
using namespace std;
typedef complex<double> Point, Vector;
typedef pair<Point, Point> Segment, Line;
typedef vector<Point> Polygon;
#define X real()
#define Y imag()
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)
double dot(Vector a, Vector b){
return a.X * b.X + a.Y * b.Y;
}
double cross(Vector a, Vector b){
return a.X * b.Y - a.Y * b.X;
}
static const int COUNTER_CLOCKWISE = 1;
static const int CLOCKWISE = -1;
static const int ONLINE_BACK = 2;
static const int ONLINE_FRONT = -2;
static const int ON_SEGMENT = 0;
// ベクトルと点の位置関係
int ccw(Point p0, Point p1, Point p2){
Vector a = p1 - p0;
Vector b = p2 - p0;
if(cross(a, b) > EPS) return COUNTER_CLOCKWISE;
if(cross(a, b) < -EPS) return CLOCKWISE;
if(dot(a, b) < -EPS) return ONLINE_BACK;
if(norm(a) < norm(b)) return ONLINE_FRONT;
return ON_SEGMENT;
}
bool isConvex(Polygon pol){
int n = pol.size();
for(int i=0; i<n; i++){
if(ccw(pol[i], pol[(i+1)%n], pol[(i+2)%n]) == CLOCKWISE){
return false;
}
}
return true;
}
int main(){
int n; cin>>n;
Polygon pol;
for(int i=0; i<n; i++){
double x, y; cin>>x>>y;
pol.emplace_back(x, y);
}
cout << isConvex(pol) << endl;
}
|
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>
#include <cmath>
#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair
#define EPS 1e-8
#define equals(a,b) fabs((a) - (b)) < EPS
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
struct Point {
double x, y;
Point(double x=0, double y=0) : x(x), y(y) {}
Point operator+(const Point &o) const { return Point(x+o.x, y+o.y); }
Point operator-(const Point &o) const { return Point(x-o.x, y-o.y); }
Point operator*(const double m) const { return Point(x*m, y*m); }
Point operator/(const double d) const { return Point(x/d, y/d); }
bool operator<(const Point &o) const { return x != o.x ? x < o.x : y < o.y; }
bool operator==(const Point &o) const { return fabs(x-o.x) < EPS && fabs(y-o.y) < EPS; }
};
ostream& operator << (ostream& os, const Point& p) {
os << "(" << p.x << ", " << p.y << ")";
return os;
}
double dot(Point a, Point b) { return a.x * b.x + a.y * b.y; }
double cross(Point a, Point b) { return a.x * b.y - a.y * b.x; }
double atan(Point p) { return atan2(p.y, p.x); }
double norm(Point p) { return p.x * p.x + p.y * p.y; }
double abs(Point p) { return sqrt(norm(p)); }
double distancePP(Point p, Point o) { return sqrt(norm(o - p)); }
int ccw(Point a, Point b, Point c) {
b = b-a;
c = c-a;
if(cross(b, c) > 0.0) return +1; //conter clockwise
if(cross(b, c) < 0.0) return -1; //clockwise
if(dot(b, c) < 0.0) return +2; //a on Seg(b,c)
if(norm(b) < norm(c)) return -2; //b on Seg(a,c)
return 0; //c on Seg(a,b)
}
double polygonArea(const vector<Point>& p) {
int n = p.size();
double ret = 0.0;
for(int i = 0; i < n; ++i) {
ret += cross(p[i], p[(i+1)%n]);
}
return abs(ret) / 2.0;
}
bool isConvex(const vector<Point>& p) {
int n = p.size();
rep(i, n) {
if(ccw(p[i], p[(i+1)%n], p[(i+2)%n]) == -1) return false;
}
return true;
}
struct Line {
Point a, b;
Line() : a(Point(0, 0)), b(Point(0, 0)) {}
Line(Point a, Point b) : a(a), b(b) {}
};
ostream& operator << (ostream& os, const Line& l) {
os << "(" << l.a.x << ", " << l.a.y << ")-(" << l.b.x << "," << l.b.y << ")";
return os;
}
struct Seg {
Point a,b;
Seg() : a(Point(0, 0)), b(Point(0, 0)) {}
Seg (Point a, Point b) : a(a),b(b) {}
};
ostream& operator << (ostream& os, const Seg& s) {
os << "(" << s.a.x << ", " << s.a.y << ")-(" << s.b.x << "," << s.b.y << ")";
return os;
}
bool isOrthogonal(Line l1, Line l2) { return equals(dot((l1.b - l1.a), (l2.b - l2.a)), 0.0); }
bool isParallel(Line l1, Line l2) { return equals(cross((l1.b - l1.a), (l2.b - l2.a)), 0.0); }
bool sameLine(Line l1, Line l2) { return abs(cross(l1.b - l1.a, l2.b - l1.a)) < EPS; }
bool isIntersectLL(Line l1, Line l2) { return !isParallel(l1, l2) || sameLine(l1, l2); }
bool isIntersectLS(Line l, Seg s) {
return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < 0;
}
bool isIntersectSS(Seg s1, Seg s2) {
return ccw(s1.a, s1.b, s2.a) * ccw(s1.a, s1.b, s2.b) <= 0
&& ccw(s2.a, s2.b, s1.a) * ccw(s2.a, s2.b, s1.b) <= 0;
}
double distanceLP(Line l, Point p) {
return abs(cross(l.b - l.a, p - l.a)) / abs(l.b - l.a);
}
double distanceLS(Line l, Seg s) {
if (isIntersectLS(l, s)) return 0.0;
return min(distanceLP(l, s.a), distanceLP(l, s.b));
}
double distanceSP(Seg s, Point p) {
if (dot(s.b - s.a, p - s.a) < 0.0) return abs(p - s.a);
if (dot(s.a - s.b, p - s.b) < 0.0) return abs(p - s.b);
return distanceLP(Line(s.a, s.b) , p);
}
double distanceSS(Seg s1, Seg s2) {
if (isIntersectSS(s1, s2)) return 0.0;
return min( min(distanceSP(s1, s2.a), distanceSP(s1, s2.b)), min(distanceSP(s2, s1.a), distanceSP(s2, s1.b)) );
}
// if isIntersectLL(l1, l2)
Point crossPointLL(Line l1, Line l2) {
Point base = l2.b - l2.a;
double d = abs(cross(base, l1.a - l2.a));
double d2 = abs(cross(base, l1.b - l2.a));
double t = d / (d + d2);
return l1.a + (l1.b - l1.a) * t;
}
// if isIntersectLS(l, s)
Point crossPointLS(Line l, Seg s) { return crossPointLL(l, Line(s.a, s.b)); }
// if isIntersectSS(s1, s2)
Point crossPointSS(Seg s1, Seg s2) { return crossPointLL(Line(s1.a, s1.b), Line(s2.a, s2.b)); }
Point project(Line l, Point p) {
Point base = l.b - l.a;
double t = dot(base, p-l.a) / dot(base, base);
return l.a + base * t;
}
Point reflect(Line l, Point p) {
return p + (project(l, p) - p) * 2.0;
}
int main() {
int n;
cin >> n;
vector<Point> v(n);
rep(i, n) cin >> v[i].x >> v[i].y;
if(isConvex(v)) {
cout << 1 << endl;
} else {
cout << 0 << endl;
}
return 0;
} |
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include<bits/stdc++.h>
#define INF 1e9
#define EPS 1e-9
#define REP(i,n) for(lint i=0,i##_len=(n);i<i##_len;++i)
#define REP1(i,n) for(lint i=1,i##_len=(n);i<=i##_len;++i)
#define REPR(i,n) for(lint i=(n)-1;i>=0;--i)
#define REPR1(i,n) for(lint i=(n);i>0;--i)
#define REPC(i,obj) for(auto i:obj)
#define R_UP(a,b) (((a)+(b)-1)/(b))
#define ALL(obj) (obj).begin(),(obj).end()
#define SETP cout << fixed << setprecision(10)
using namespace std;
using lint = long long;
template<typename T = lint>inline T in() { T x; cin >> x; return x; }
class vec2d;
class line2d;
class vec2d {
public:
double x, y;
vec2d(double, double);
vec2d operator+(const vec2d&)const;
vec2d operator-(const vec2d&)const;
vec2d operator*(double)const;
vec2d operator/(double)const;
bool operator==(const vec2d&)const;
bool operator!=(const vec2d&)const;
double norm() const;
static double inner_product(const vec2d&, const vec2d&);
static double cros_product(const vec2d&, const vec2d&);
static double argument(const vec2d&, const vec2d&, bool = true);
static double distance(const vec2d&, const vec2d&);
static vec2d projection(const vec2d&, const vec2d&);
static vec2d reflection(const vec2d&, const line2d&);
};
class line2d {
public:
vec2d v0, v1;
line2d(const vec2d&, const vec2d&);
double length()const;
static vec2d cross_point(const line2d&, const line2d&);
static bool is_cross(const line2d&, const line2d&);
static double distance(const vec2d&, const line2d&);
static double distance(const line2d&, const line2d&);
};
vec2d::vec2d(double x, double y) :x(x), y(y) {}
vec2d vec2d::operator+(const vec2d&v)const {
return vec2d(x + v.x, y + v.y);
}
vec2d vec2d::operator-(const vec2d&v)const {
return vec2d(x - v.x, y - v.y);
}
vec2d vec2d::operator*(double k)const {
return vec2d(x * k, y * k);
}
vec2d vec2d::operator/(double k)const {
return vec2d(x / k, y / k);
}
bool vec2d::operator==(const vec2d&v)const {
return x == v.x && y == v.y;
}
bool vec2d::operator!=(const vec2d&v)const {
return !operator== (v);
}
double vec2d::norm() const {
return sqrt(x * x + y * y);
}
double vec2d::inner_product(const vec2d&v0, const vec2d&v1) {
return v0.x * v1.x + v0.y * v1.y;
}
double vec2d::cros_product(const vec2d&v0, const vec2d&v1) {
return v0.x * v1.y - v0.y * v1.x;
}
double vec2d::argument(const vec2d&v0, const vec2d&v1, bool is_signed) {
double ret = atan2(cros_product(v0, v1), inner_product(v0, v1));
return is_signed ? ret : abs(ret);
}
double vec2d::distance(const vec2d&v0, const vec2d&v1) {
return hypot(v1.x - v0.x, v1.y - v0.y);
}
vec2d vec2d::projection(const vec2d&scr, const vec2d&v) {
double n = scr.norm();
return scr * vec2d::inner_product(scr, v) / n / n;
}
vec2d vec2d::reflection(const vec2d&v, const line2d&l) {
return (projection(vec2d(l.v1 - l.v0), vec2d(v - l.v0)) + l.v0) * 2 - v;
}
line2d::line2d(const vec2d&v0, const vec2d&v1) :v0(v0), v1(v1) {}
double line2d::length()const {
return vec2d::distance(v0, v1);
}
vec2d line2d::cross_point(const line2d&l0, const line2d&l1) {
return vec2d(((l0.v1.x * l0.v0.y - l0.v0.x * l0.v1.y) * (l1.v1.x - l1.v0.x) - (l1.v1.x * l1.v0.y - l1.v0.x * l1.v1.y) * (l0.v1.x - l0.v0.x)) / ((l0.v1.x - l0.v0.x) * (l1.v1.y - l1.v0.y) - (l1.v1.x - l1.v0.x) * (l0.v1.y - l0.v0.y)), ((l0.v1.x * l0.v0.y - l0.v0.x * l0.v1.y) * (l1.v1.y - l1.v0.y) - (l1.v1.x * l1.v0.y - l1.v0.x * l1.v1.y) * (l0.v1.y - l0.v0.y)) / ((l0.v1.x - l0.v0.x) * (l1.v1.y - l1.v0.y) - (l1.v1.x - l1.v0.x) * (l0.v1.y - l0.v0.y)));
}
bool line2d::is_cross(const line2d&l0, const line2d&l1) {
vec2d cross = cross_point(l0, l1);
if (std::isnan(cross.x) && std::isnan(cross.y)) {
double dis_sum = vec2d::distance(l0.v0, l0.v1) + vec2d::distance(l1.v0, l1.v1);
double dis_max = max({ vec2d::distance(l0.v0, l0.v1),vec2d::distance(l0.v0, l1.v0),vec2d::distance(l0.v0, l1.v1),vec2d::distance(l0.v1, l1.v0),vec2d::distance(l0.v1, l1.v1),vec2d::distance(l1.v0, l1.v1) });
return dis_max < dis_sum || abs(dis_max - dis_sum) < EPS;
} else if (std::isinf(cross.x) || std::isinf(cross.y)) {
return false;
} else {
vec2d v_l0 = l0.v1 - l0.v0;
vec2d cross_l0 = cross - l0.v0;
double ks0 = v_l0.x != 0 ? cross_l0.x / v_l0.x : cross_l0.y / v_l0.y;
vec2d v_l1 = l1.v1 - l1.v0;
vec2d cross_l1 = cross - l1.v0;
double ks1 = v_l1.x != 0 ? cross_l1.x / v_l1.x : cross_l1.y / v_l1.y;
return 0 <= ks0 && ks0 <= 1 && 0 <= ks1 && ks1 <= 1;
}
}
double line2d::distance(const vec2d&v, const line2d&l) {
line2d l_ref(v, vec2d::reflection(v, l));
return is_cross(l, l_ref) ? l_ref.length() / 2 : min({ vec2d::distance(v,l.v0),vec2d::distance(v,l.v1) });
}
double line2d::distance(const line2d&l0, const line2d&l1) {
return is_cross(l0, l1) ? 0 : min({ distance(l0.v0,l1),distance(l0.v1,l1),distance(l1.v0,l0),distance(l1.v1,l0) });
}
class polygon {
public:
vector<vec2d>vartex;
polygon(int n) {
vartex.reserve(n);
}
void push_back(vec2d v) {
vartex.push_back(v);
}
double area() {
double ret = 0;
int num = vartex.size();
REP(i, num) {
ret += vec2d::cros_product(vartex[i], vartex[(i + 1) % num]);
}
return ret / 2;
}
bool is_convex() {
int num = vartex.size();
REP(i, num) if (vec2d::cros_product(vartex[(i + 1) % num] - vartex[i], vartex[(i + 2) % num] - vartex[(i + 1) % num]) < 0)return false;
return true;
}
};
signed main() {
int n = in();
polygon pol(n);
REP(i, n) {
double x = in(), y = in();
pol.push_back(vec2d(x, y));
}
cout << (pol.is_convex() ? '1' : '0') << endl;
}
|
#include <iostream>
#include <fstream>
#include <typeinfo>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <iomanip>
#include <cctype>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<25;
const double pi=acos(-1);
const double eps=1e-8;
const vi emp;
struct point{
double x,y;
point operator+(point p){
return point{x+p.x,y+p.y};
}
point operator-(point p){
return point{x-p.x,y-p.y};
}
point operator*(double p){
return point{x*p,y*p};
}
point operator/(double p){
if(!p) return point{0,0};
return point{x/p,y/p};
}
bool operator==(point p){
return fabs(x-p.x)<eps&&fabs(y-p.y)<eps;
}
};
typedef pair<point,point> pp;
typedef vector<point> VP;
const point O{0,0};
class Geom{
public:
double Length(point x,point y){
point z=y-x;
return sqrt(z.x*z.x+z.y*z.y);
}
double IP(point p,point q){
return p.x*q.x+p.y*q.y;
}
double CP(point p,point q){
return p.x*q.y-q.x*p.y;
}
int Convex_Concave(VP p){
p.push_back(p[0]);
double cp;
for(VP::iterator i=p.begin()+1;i!=p.end()-1;i++){
cp=CP(*i-*(i-1),*(i+1)-*(i-1));
if(cp<0) return 0;
}
return 1;
}
double Heron(point A,point B,point C){
double a=Length(B,C),b=Length(C,A),c=Length(A,B),s=(a+b+c)/2;
return sqrt(s*(s-a)*(s-b)*(s-c));
}
double Area(VP p){
double sum=0,cp;
p.push_back(p[0]);
for(VP::iterator i=p.begin()+1;i!=p.end();i++){
cp=CP(*(i-1),*i);
if(cp>0) sum+=Heron(O,*(i-1),*i);
else if(cp<0) sum-=Heron(O,*(i-1),*i);
}
return sum;
}
/* string Containment_polyon_point(VP p,point x){
string s;
p.push_back(p[0]);
double sum,area=Area(p);
for(VP::iterator i=p.begin()+1;i!=p.end();i++){
}
}*/
void Point_in(point& p){
cin>>p.x>>p.y;
}
};
int N;
VP p;
point q;
int main(){
cout<<syosu(1);
Geom geo;
cin>>N;
p=VP(N);
for(VP::iterator i=p.begin();i!=p.end();i++) geo.Point_in(*i);
/* cin>>Q;
for(int i=0;i<Q;i++){
geo.Point_in(q);
string s=geo.Containment_polyon_point(p,q);
if(s=="Containment") cout<<2<<endl;
else if(s=="On_side") cout<<1<<endl;
else cout<<0<<endl;
}*/
cout<<geo.Convex_Concave(p)<<endl;
} |
#include<bits/stdc++.h>
typedef long double lf;
using namespace std;
const lf EPS = 1e-9;
const lf oo = 1e15;
struct pt {
lf x, y;
pt( ) { }
pt( lf x, lf y ) : x( x ), y ( y ) { }
};
inline lf x( pt P ) { return P.x; }
inline lf y( pt P ) { return P.y; }
istream& operator >> ( istream& in, pt& p ) {
lf x,y; in >> x >> y;
p = pt(x,y); return in;
}
ostream& operator << ( ostream& out, const pt& p ) {
out << double(p.x) << " " << double(p.y);
return out;
}
pt operator + ( const pt& A, const pt& B ) {
return pt( x(A)+x(B), y(A)+y(B) );
}
pt operator - ( const pt& A, const pt& B ) {
return pt( x(A)-x(B), y(A)-y(B) );
}
pt operator * ( const lf& B, const pt& A ) {
return pt( x(A)*B, y(A)*B );
}
pt operator * ( const pt& A, const lf& B ) {
return pt( x(A)*B, y(A)*B );
}
inline lf dot( pt A, pt B ) {
return x(A)*x(B) + y(A)*y(B);
}
inline lf norm( pt A ) {
return x(A)*x(A) + y(A)*y(A);
}
inline lf abs( pt A ) {
return sqrt( norm(A) );
}
inline lf dist ( pt A, pt B ) { return abs( B - A ); }
lf distToLine (pt p, pt A, pt B, pt &c) {
lf u = dot( p-A , B-A ) / norm( B-A );
c = A + u*( B-A );
return dist( p , c );
}
pt refPoint(pt X, pt A, pt B) {
pt aux; distToLine(X, A, B, aux);
return X + lf(2.0)*(aux-X);
}
inline bool same ( lf a, lf b ) {
return a+EPS > b && b+EPS > a;
}
inline lf cross( pt A, pt B ) {
return x(A)*y(B) - y(A)*x(B);
}
///CHANGE
// 0 for collineal points ( angle = 0 )
// 1 for angle BAX counter clockwise
// -1 for angle BAX clockwise
inline int ccw (pt X, pt A, pt B) {
lf c = cross( B-A, X-A );
if( same( c, 0.0 ) ) { return 0; }
if( c > EPS ) { return 1; }
return -1;
}
///CHANGE
inline bool segContains ( pt X, pt A, pt B) {
if ( !same ( 0, cross ( A-X, B-X ) ) ) return 0;
return ( dot ( A-X, B-X ) < EPS );
}
inline bool parallel( pt A, pt B, pt C, pt D ) {
return same ( 0, cross( B-A, D-C ) );
}
///NEW
inline bool ortho( pt A, pt B, pt C, pt D ) {
return same ( 0, dot( B-A, D-C ) );
}
inline bool samePt ( pt A, pt B ) {
return same ( x(A), x(B) ) && same ( y(A), y(B) );
}
pt linesIntersection ( pt A, pt B, pt C, pt D ) {
lf x = cross ( C, D-C ) - cross ( A, D-C );
x /= cross ( B-A, D-C );
return A + x*(B-A);
}
inline bool collinearSegsIntersects ( pt A, pt B, pt C, pt D ) {
return segContains(A,C,D) || segContains(B,C,D)
|| segContains(C,A,B) || segContains(D,A,B);
}
bool segmentsIntersect(pt A, pt B, pt C, pt D) {
if( samePt(A,B) )
return segContains( A, C, D );
if( samePt(C,D) )
return segContains( C, A, B );
if( parallel(A,B,C,D) )
return collinearSegsIntersects( A,B,C,D );
pt aux = linesIntersection(A,B,C,D);
return segContains(aux,A,B) && segContains(aux,C,D);
}
///CHANGE
lf distToSegment(pt p, pt A, pt B, pt &c) {
lf u = dot( p-A , B-A ) / norm( B-A );
if( u < -EPS ) { c = A; return dist( p , A ); }
if( (u-1.0) > EPS ) { c = B; return dist( p, B ); }
return distToLine(p,A,B,c);
}
// P[0] must be equal to P[n]
// Area is positive if the polygon is ccw
double signedArea(const vector<pt> &P) {
double result = 0.0;
for(int i = 0; i < (int)P.size()-1; i++) result += cross( P[i],P[i+1] );
return result / 2.0;
}
double area(const vector<pt> &P) { return fabs(signedArea(P)); }
/// --------------------------------------------
///CHANGE
// P[0] must be equal to P[n]
bool isConvex( const vector<pt> &P) {
int sz = (int) P.size(); if(sz <= 3) return false;
bool isL = ccw(P[0], P[1], P[2]) >= 0;
for (int i = 1; i < sz-1; i++) {
if( ( ccw(P[i], P[i+1], P[(i+2) == sz ? 1 : i+2]) >= 0 ) != isL) return false;
}
return true;
}
int n;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(1);
cin >> n;
vector<pt> P( n+1 );
for( int i = 0; i < n; ++i ) {
cin >> P[i];
}
P[n] = P[0];
if( isConvex(P) )
cout << "1\n";
else
cout << "0\n";
} |
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
#define rep(i, n) for (int64_t i = 0; i < (n); i++)
#define irep(i, n) for (int64_t i = 0; i <= (n); i++)
#define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--)
#define rirep(i, n) for (int64_t i = n; i >= 0; i--)
class Vec;
Vec operator/(const Vec& v, const double& divisor);
Vec operator*(const double& scale, const Vec& v);
class Vec {
using F = double;
vector<F> container;
public:
Vec(size_t size) : container(size) {}
Vec(initializer_list<F> elements)
: container(elements.begin(), elements.end()) {}
size_t dim() const { return container.size(); }
F& operator[](const size_t size) { return container.at(size); }
const F& operator[](const size_t size) const { return container.at(size); }
Vec operator+(const Vec& other) const {
Vec ret(*this);
for (size_t i = 0; i < dim(); i++) ret[i] += other[i];
return ret;
}
Vec operator-(const Vec& other) const { return (*this) + (-other); }
Vec operator-() const { return -1.0 * (*this); }
F inner(const Vec& other) const {
F acc = 0;
for (size_t i = 0; i < dim(); i++) {
acc += (*this)[i] * other[i];
}
return acc;
}
F norm() const { return sqrt(inner(*this)); }
Vec unit() const { return (*this) / this->norm(); }
F ccw(const Vec& other) const {
return (*this)[0] * other[1] - other[0] * (*this)[1];
}
Vec normal() const { return Vec({-(*this)[1], (*this)[0]}); }
};
Vec operator/(const Vec& v, const double& divisor) { return 1.0 / divisor * v; }
Vec operator*(const double& scale, const Vec& v) {
Vec ret(v);
for (size_t i = 0; i < ret.dim(); i++) ret[i] *= scale;
return ret;
}
class Polygon {
const vector<Vec>& vert;
public:
Polygon(const vector<Vec>& vert) : vert(vert) {}
double area() const {
const size_t n = vert.size();
double acc = 0;
for (size_t i = 0; i < n; i++) {
acc += vert[i].ccw(vert[(i + 1) % n]);
}
return acc / 2.0;
}
bool isConvex(double eps) const {
const size_t n = vert.size();
for (size_t i = 0; i < n; i++) {
const Vec a = vert[(i + 1) % n] - vert[i],
b = vert[(i + 2) % n] - vert[i];
const double c = a.ccw(b);
if (c < -eps) {
return false;
}
}
return true;
}
};
int main() {
int n;
cin >> n;
vector<Vec> v;
rep(i, n) {
double x, y;
cin >> x >> y;
v.push_back(Vec({x, y}));
}
const double EPS = 1e-9;
cout << (Polygon(v).isConvex(EPS) ? 1 : 0) << endl;
return 0;
}
|
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 1e4;
const double eps = 1e-8;
const double inf = 1e20;
inline double sqr (double k) {return k * k;}
inline int sgn (double p) {
if (fabs (p) < eps) return 0;
if (p < 0) return -1;
return 1;
}
struct point {
double x, y;
point () {}
point (double _x, double _y) {
x = _x, y = _y;
}
void input () {
scanf ("%lf %lf", &x, &y);
}
bool operator == (point b) const {
return sgn (x - b.x) == 0 && sgn (y - b.y) == 0;
}
point operator - (const point &b) const {
return point (x - b.x, y - b.y);
}
double operator * (const point &b) const {
return x * b.x + y * b.y;
}
double operator ^ (const point &b) const {
return x * b.y - y * b.x;
}
double len () {
return hypot (x, y);
}
double len2 () {
return x * x + y * y;
}
double distance (point p) {
return hypot (x - p.x, y - p.y);
}
point operator + (const point &b) const {
return point (x + b.x, y + b.y);
}
point operator * (const double &b) const {
return point (x * b, y * b);
}
point operator / (const double &b) const {
return point (x / b, y / b);
}
double cross (point a, point b) {///叉积
return (a - *this) ^ (b - *this);
}
double dot (point a, point b) {///点积
return (a - *this) * (b - *this);
}
bool on_seg (point a, point b) {///点是否在线段ab上
return sgn (cross (a, b)) == 0 && dot (a, b) <= 0;
}
} ps[N];
///两线段是否相交
bool seg (point a, point b, point c, point d) {
if (a.on_seg (c, d) || b.on_seg (c, d) || c.on_seg (a, b) || d.on_seg (a, b)) return true;
if (a.cross (b, d) * a.cross (b, c) < 0 && c.cross (d, b) * c.cross (d, a) < 0) return true;
return false;
}
/// 点的投影是否在线段ab上
bool check (point p, point a, point b) {
point x = b - a;
double k = x.len ();
double u = a.dot (p, b) / k;
x = (x / k) * u;
x = x + a;
if (x.on_seg (a, b)) return true;
return false;
}
int cmp (point a, point b) {
if ((a ^ b) > 0) return true;
return false;
}
int main () {
int n, ans = 1;
cin >> n;
for (int i = 1; i <= n; i++) ps[i].input ();
// for (int i = 1; i <= n; i++) cout << ps[i].x << ' ' << ps[i].y << endl;
ps[0] = ps[n];
ps[n + 1] = ps[1];
for (int i = 1; i <= n; i++) {
if (ps[i].cross (ps[i - 1], ps[i + 1]) > 0) ans = 0;
// cout << i << ' ' << ps[i].cross (ps[i - 1], ps[i + 1]) << ' ' << ps[i].x << ' ' << ps[i].y << endl;
}
cout << ans << endl;
return 0;
}
|
#include <iostream>
#include <cmath>
#include <string>
#include <cstdio>
#include <algorithm>
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
//点を表す構造体
class Point{
public:
double x, y;
Point(double x=0, double y=0){
this->x = x;
this->y = y;
}
Point operator+(const Point &seg2){
return Point(x+seg2.x, y+seg2.y);
}
Point operator-(const Point &seg2){
return Point(x-seg2.x, y-seg2.y);
}
Point operator*(const double k){
return Point(x*k, y*k);
}
Point &operator=(const Point &p){
x = p.x;
y = p.y;
return *this;
}
bool operator < (const Point &p) const{
return x != p.x? x<p.x : y<p.y;
}
bool operator == (const Point &p) const{
return equals(x, p.x) && equals(y, p.y);
}
};
//ベクトルを点の別表記でも表せるようにしておく
class Vector : public Point{
public:
Vector() : Point() {}
Vector(double x, double y) : Point(x, y) {}
Vector(Point p) : Point(){
x = p.x;
y = p.y;
}
double norm(){
return x*x + y*y;
}
double abs(){
return sqrt(norm());
}
static double dot(Vector a, Vector b){
return a.x*b.x + a.y*b.y;
}
static double cross(Vector a, Vector b){
return a.x*b.y - a.y*b.x;
}
static bool isOrthogonal(Vector a, Vector b){
return equals(dot(a, b), 0.0);
}
static bool isParallel(Vector a, Vector b){
return equals(cross(a, b), 0.0);
}
};
//線分(ベクトルを用いて)を表す構造体。両端の点が定義されている点に注意
class Segment{
public:
Point p1, p2;
Segment(Point p1, Point p2){
this->p1 = p1;
this->p2 = p2;
x = p2.x-p1.x;
y = p2.y-p1.y;
}
static bool isOrthogonal(Segment a, Segment b){
return equals(dot(a, b), 0.0);
}
static bool isParallel(Segment a, Segment b){
return equals(cross(a, b), 0.0);
}
private:
int x, y;
static double dot(Segment a, Segment b){
return a.x*b.x + a.y*b.y;
}
static double cross(Segment a, Segment b){
return a.x*b.y - a.y*b.x;
}
};
typedef Segment Line;
class Circle{
public:
Point c;
double r;
Circle(Point c = Point(), double r = 0.0): c(c), r(r){}
};
class Polygon{
public:
Point* ver;
int size;
Polygon(int size){
this->size = size;
ver = new Point[size];
}
~Polygon(){
delete[] ver;
}
double area(){
double ans=0.0;
Vector a, b;
for(int i=0; i<size; i++){
a=ver[i%size]; b=ver[(i+1)%size];
ans += Vector::cross(a, b)/2;
}
return ans;
}
bool isConvex(){
bool isC = true;
for(int i=0; i<size; i++){
Vector a = ver[(i+1)%size]-ver[i%size], b = ver[(i+2)%size]-ver[i%size];
if(Vector::cross(a, b)<-EPS){
isC = false;
break;
}
}
return isC;
}
};
class Tryangle : public Polygon{
public:
Tryangle(Point p1, Point p2, Point p3) : Polygon(3){
ver[0] = p1;
ver[1] = p2;
ver[3] = p3;
}
double area(){
Vector a = ver[1]-ver[0], b = ver[2]-ver[0];
return fabs(Vector::cross(a, b))/2.0;
}
};
string ccw_str(Point p0, Point p1, Point p2){
//enum res {COUNTER_CLOCKWISE=0, CLOCKWISE, ONLINE_BACK, ONLINE_FRONT, ON_SEGMENT};
Vector a = p1-p0;
Vector b = p2-p0;
if(Vector::cross(a, b) > EPS) return "COUNTER_CLOCKWISE";
if(Vector::cross(a, b) < -EPS) return "CLOCKWISE";
if(Vector::dot(a, b) < -EPS) return "ONLINE_BACK";
if(a.norm() < b.norm()) return "ONLINE_FRONT";
return "ON_SEGMENT";
}
int ccw_int(Point p0, Point p1, Point p2){
enum res {COUNTER_CLOCKWISE=-1, CLOCKWISE=1, ONLINE_BACK=2, ONLINE_FRONT=-2, ON_SEGMENT=0};
Vector a = p1-p0;
Vector b = p2-p0;
if(Vector::cross(a, b) > EPS) return COUNTER_CLOCKWISE;
if(Vector::cross(a, b) < -EPS) return CLOCKWISE;
if(Vector::dot(a, b) < -EPS) return ONLINE_BACK;
if(a.norm() < b.norm()) return ONLINE_FRONT;
return ON_SEGMENT;
}
Point project(Segment s, Point p){
Vector base = s.p2-s.p1;
double r = Vector::dot(p - s.p1, base) / base.norm();
return s.p1 + base*r;
}
Point reflect(Segment s, Point p){
Point pro = project(s, p);
return p + (pro-p)*2.0;
}
bool intersect(Point p1, Point p2, Point p3, Point p4){
return ccw_int(p1, p2, p3)*ccw_int(p1, p2, p4)<=0 && ccw_int(p3, p4, p1)*ccw_int(p3, p4, p2)<=0;
}
bool intersect(Segment s1, Segment s2){
return intersect(s1.p1, s1.p2, s2.p1, s2.p2);
}
Point crossPoint(Point p1, Point p2, Point p3, Point p4){
Vector base = p2-p1;
double d1 = fabs(Vector::cross(base, p4-p1))/fabs(base.abs());
double d2 = fabs(Vector::cross(base, p3-p1))/fabs(base.abs());
double t = d1/(d1+d2);
Point x = p4 + (p3-p4)*t;
return x;
}
double getDistance(Point p1, Point p2){
Vector base=p2-p1;
return base.abs();
}
double getDistance(Point p, Segment s){
double dot1, dot2;
Vector base = s.p2-s.p1;
dot1 = Vector::dot(base, p-s.p1);
dot2 = Vector::dot(base*(-1.0), p-s.p2);
double ans;
if(dot1<-EPS) ans = getDistance(s.p1, p);
else if(dot2<-EPS) ans = getDistance(s.p2, p);
else ans = fabs(Vector::cross(p-s.p1, base))/base.abs();
return ans;
}
double getDistance(Segment s1, Segment s2){
double ans;
if(intersect(s1, s2)) ans = 0.0;
else {
ans = min(min(getDistance(s1.p1, s2), getDistance(s1.p2, s2)),
min(getDistance(s2.p1, s1), getDistance(s2.p2, s1)));
}
return ans;
}
int main(){
int n; cin >> n;
Polygon p(n);
double x, y;
for(int i=0; i<n; i++){
cin >> x >> y;
p.ver[i] = Point(x, y);
}
cout << p.isConvex() << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define Vector Point
#define INF 2000000000
#define DOUBLE_INF 1e50
#define sq(x) ((x)*(x))
#define fi first
#define sec second
#define all(x) (x).begin(),(x).end()
#define EQ(a,b) (abs((a)-(b))<eps)
// Geometry Library
// written by okuraofvegetable
inline double add(double a,double b){
if(abs(a+b)<eps*(abs(a)+abs(b)))return 0;
return a+b;
}
struct Point{
double x,y;
Point() {}
Point(double x,double y) : x(x),y(y){}
Point operator + (Point p){return Point(add(x,p.x),add(y,p.y));}
Point operator - (Point p){return Point(add(x,-p.x),add(y,-p.y));}
Point operator * (double d){return Point(x*d,y*d);}
double dot(Point p){return add(x*p.x,y*p.y);}
double det(Point p){return add(x*p.y,-y*p.x);}
double norm(){return sqrt(x*x+y*y);}
double norm2(){return x*x+y*y;}
double dist(Point p){return ((*this)-p).norm();}
double dist2(Point p){return sq(x-p.x)+sq(y-p.y);}
Point vert(){return Point(y,-x);}
void dump(const char* msg=""){printf("%s%.12f %.12f\n",msg,x,y);return;}
// following functions for vector operation
// signed area of triange (0,0) (x,y) (p.x,p.y)
double area(Point p){
return (x*p.y-p.x*y)/2.0;
}
};
// direction a -> b -> c
// verified AOJ CGL_1_C
int ccw(Point a,Point b,Point c){
Vector p = b-a;
Vector q = c-a;
if(p.det(q)>0.0)return 1; // counter clockwise
if(p.det(q)<0.0)return -1; // clockwise
if(p.dot(q)<0.0)return 2; // c--a--b online_back
if(p.norm()<q.norm())return 3; // a--b--c online_front
return 4;// a--c--b on_segment
}
struct Line{
Point a,b;
Line(){}
Line(Point a,Point b):a(a),b(b){}
bool on(Point q){
return (a-q).det(b-q)==0;
}
// folloing 2 functions verified AOJ CGL_2_A
bool is_parallel(Line l){return (a-b).det(l.a-l.b)==0;}
bool is_orthogonal(Line l){return (a-b).dot(l.a-l.b)==0;}
Point intersection(Line l){
//assert(!is_parallel(l));
return a+(b-a)*((l.b-l.a).det(l.a-a)/(l.b-l.a).det(b-a));
}
// projection of p to this line
// verified AOJ CGL_1_A
Point projection(Point p){
return (b-a)*((b-a).dot(p-a)/(b-a).norm2())+a;
}
// reflection point of p onto this line
// verified AOJ CGL_1_B
Point refl(Point p){
Point proj = projection(p);
return p+((proj-p)*2.0);
}
};
struct Segment{
Point a,b;
Segment(){}
Segment(Point a,Point b):a(a),b(b){}
Line line(){
return Line(a,b);
}
bool on(Point q){
return ((a-q).det(b-q)==0&&(a-q).dot(b-q)<=0);
}
// verified AOJ CGL_2_B
bool is_intersect(Segment s){
if(line().is_parallel(s.line())){
if(on(s.a)||on(s.b))return true;
if(s.on(a)||s.on(b))return true;
return false;
}
Point p = line().intersection(s.line());
if(on(p)&&s.on(p))return true;
else return false;
}
bool is_intersect(Line l){
if(line().is_parallel(l)){
if(l.on(a)||l.on(b))return true;
else return false;
}
Point p = line().intersection(l);
if(on(p))return true;
else return false;
}
// following 2 distance functions verified AOJ CGL_2_D
double distance(Point p){
double res = DOUBLE_INF;
Point q = line().projection(p);
if(on(q))res = min(res,p.dist(q));
res = min(res,min(p.dist(a),p.dist(b)));
return res;
}
double distance(Segment s){
if(is_intersect(s))return 0.0;
double res = DOUBLE_INF;
res = min(res,s.distance(a));
res = min(res,s.distance(b));
res = min(res,this->distance(s.a));
res = min(res,this->distance(s.b));
return res;
}
};
typedef vector<Point> Polygon;
// verified AOJ CGL_3_A
double area(Polygon& pol){
vector<Point> vec;
double res = 0.0;
int M = pol.size();
for(int i=0;i<M;i++){
res += (pol[i]-pol[0]).area(pol[(i+1)%M]-pol[0]);
}
return res;
}
// for input
Point input_point(){
Point p;
cin >> p.x >> p.y;
return p;
}
Segment input_segment(){
Point a,b;
a = input_point();
b = input_point();
return Segment(a,b);
}
Line input_line(){
Point a,b;
a = input_point();
b = input_point();
return Line(a,b);
}
int main(){
int n;
cin >> n;
Polygon pol;
for(int i=0;i<n;i++){
pol.pb(input_point());
}
for(int i=0;i<n-1;i++){
if(ccw(pol[i],pol[i+1],pol[(i+2)%n])==-1){
cout << 0 << endl;
return 0;
}
}
cout << 1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
const ld EPS = 1e-9;
const ld INF = 1e10;
const ld PI = M_PI;
struct Point{
ld x, y;
Point(ld x, ld y):x(x), y(y){}
Point(){}
};
Point operator+(const Point &a, const Point &b){
return Point(a.x + b.x, a.y + b.y);
}
Point operator-(const Point &a, const Point &b){
return Point(a.x - b.x, a.y - b.y);
}
Point operator*(const Point &a, const ld b){
return Point(a.x * b, a.y * b);
}
Point operator*(const Point &a, const Point &b){
return Point(a.x*b.x-a.y*b.y, a.x*b.y+a.y*b.x);
}
Point operator/(const Point &a, const ld b){
return Point(a.x / b, a.y / b);
}
ld cross(const Point &a, const Point &b){
return a.x*b.y - a.y*b.x;
}
ld dot(const Point &a, const Point &b){
return a.x*b.x + a.y*b.y;
}
ld norm(const Point &a){
return dot(a, a);
}
struct Line:vector<Point>{
Line(Point a = Point(0, 0), Point b = Point(0, 0)){
this->push_back(a);
this->push_back(b);
}
};
typedef vector<Point> Polygon;
int ccw(Point a, Point b, Point c){
b = b - a;
c = c - a;
if(cross(b, c) > EPS) return +1; // 反時計周り
if(cross(b, c) < -EPS) return -1; // 時計周り
if(dot(b, c) < 0) return +2; // c -- a -- b がこの順番に一直線上
if(norm(b) < norm(c)) return -2; // a -- b -- c がこの順番に一直線上
return 0; // a -- c -- b が一直線上
}
Point next(const Polygon &a, int x){
return a[(x+1)%a.size()];
}
Point prev(const Polygon &a, int x){
return a[(x-1+a.size()) % a.size()];
}
bool is_convex(const Polygon &poly){
for(int i = 0 ; i < (int)poly.size() ; i++){
if(ccw(prev(poly, i), poly[i], next(poly, i)) == -1) return false;
}
return true;
}
int main(){
int q;
cin >> q;
Polygon poly;
while(q--){
Point p;
cin >> p.x >> p.y;
poly.push_back(p);
}
if(is_convex(poly)) cout << 1 << endl;
else cout << 0 << endl;
return 0;
} |
#include<bits/stdc++.h>
#define EPS 1e-10
#define equals(a,b) (fabs((a)-(b)) < EPS)
#define rep(i,n) for(int i=0;i<n;++i)
typedef long long ll;
using namespace std;
static const int COUNTER_CLOCKWISE = 1;
static const int CLOCKWISE = -1;
static const int ONLINE_BACK = 2;
static const int ONLINE_FRONT = -2;
static const int ON_SEGMENT = 0;
// 点
struct Point {
double x,y;
Point(){}
Point(double x, double y) : x(x),y(y){}
Point operator+(Point p) {return Point(x+p.x, y+p.y);}
Point operator-(Point p) {return Point(x-p.x, y-p.y);}
Point operator*(double k){return Point(x*k,y*k);}
Point operator/(double k){return Point(x/k,y/k);}
double norm(){return x*x+y*y;}
double abs(){sqrt(norm());}
bool operator == (const Point &p) const {return equals(x,p.x)&&equals(y,p.y);}
};
typedef Point P;
typedef vector<Point> Polygon;
double cross(Point a, Point b) {return a.x*b.y-a.y*b.x;}
double area(Polygon s){
double res=0;
for(int i=0;i<(int)s.size();i++){
res+=cross(s[i],s[(i+1)%s.size()])/2.0;
}
return res;
}
// 内積
double dot(Point a, Point b) {return a.x*b.x + a.y*b.y;}
// 2直線の直行判定
bool is_orthogonal(Point a1, Point a2, Point b1, Point b2) {
return equals(dot(a1-a2, b1-b2), 0.0);
}
// 2直線の平行判定
bool is_parallel(Point a1, Point a2, Point b1, Point b2) {
return equals(cross(a1-a2, b1-b2), 0.0);
}
// 点cが直線ab上にあるかないか
bool is_point_on_INF_line(Point a, Point b, Point c) {
return equals(cross(b-a,c-a), 0.0);
}
// 点cが線分ab上にあるかないか
bool is_point_on_LIMITED_line(Point a, Point b, Point c) {
return (Point(a-c).abs()+Point(c-b).abs() < Point(a-b).abs() + EPS);
}
// 直線と点の距離
double distance_l_p(Point a, Point b, Point c) {return abs(cross(b-a, c-a)) / (b-a).abs();}
// 点a,bを端点とする線分と点cとの距離
double distance_ls_p(Point a, Point b, Point c) {
if (dot(b-a, c-a) < EPS) return (c-a).abs();
if (dot(a-b, c-b) < EPS) return (c-b).abs();
return abs(cross(b-a, c-a)) / (b-a).abs();
}
// 点が線分のどちら側にあるかを計算
int ccw(Point p0,Point p1,Point p2) {
P a = p1-p0;
P b = p2-p0;
if(cross(a,b) > EPS) return COUNTER_CLOCKWISE;
if(cross(a,b) < -EPS) return CLOCKWISE;
if(dot(a,b) < -EPS) return ONLINE_BACK;
if(a.norm()<b.norm()) return ONLINE_FRONT;
return ON_SEGMENT;
}
bool isConvex(Polygon p){
bool f=1;
int n=p.size();
for(int i=0;i<n;i++){
int t=ccw(p[(i+n-1)%n],p[i],p[(i+1)%n]);
f&=t!=CLOCKWISE;
}
return f;
}
int main() {
int n;cin>>n;
Polygon pl;
rep(i,n) {
double x,y; cin>>x>>y;
pl.push_back(Point(x,y));
}
if (isConvex(pl)) cout << 1 << endl;
else cout << 0 << endl;
}
|
#include <bits/stdc++.h>
//#define int long long
using namespace std;
using LL = long long;
using P = pair<int, int>;
using Tapris = tuple<int, int, int>;
#define FOR(i, a, n) for(int i = (int)(a); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()
const int INF = (int)1e9;
const LL INFL = (LL)1e15;
const int MOD = 1e9 + 7;
int dy[]={0, 0, 1, -1, 0};
int dx[]={1, -1, 0, 0, 0};
typedef long double LD;
typedef complex<LD> Point;
typedef pair<Point, Point> Line;
typedef vector<Point> Polygon;
const LD EPS = 1e-10;
#define X real() // x座標を取得
#define Y imag() // y座標を取得
#define LE(n,m) ((n) < (m) + EPS)
#define GE(n,m) ((n) + EPS > (m))
#define EQ(n,m) (abs((n)-(m)) < EPS)
// 内積 Dot(a, b) = |a||b|cosθ
LD Dot(Point a, Point b){
return (conj(a)*b).X;
}
// 外積 Cross(a, b) = |a||b|sinθ
LD Cross(Point a, Point b){
return (conj(a)*b).Y;
}
int CCW(Point a, Point b, Point c){
b -= a; c -= a;
if (Cross(b, c) > 0) return +1; // counter clockwise
if (Cross(b, c) < 0) return -1; // clockwise
if (Dot(b, c) < 0) return +2; // c--a--b on line
if (norm(b) < norm(c)) return -2; // a--b--c on line
return 0;
}
//*********************************************
// 点と線(Point and Line) *
//*********************************************
// 交差判定 (Isec) ****************************
// 点 := 平面座標にある点
// 直線 := 点と点を通るどこまでも続く線
// 線分 := 点と点を結んでその両端で止まっている線
// 直線と点
bool IsecLP(Point a1, Point a2, Point b){
return abs(CCW(a1, a2, b)) != 1;
}
// 直線と直線
bool IsecLL(Point a1, Point a2, Point b1, Point b2) {
return !IsecLP(a2-a1, b2-b1, 0) || IsecLP(a1, b1, b2);
}
// 直線と線分
bool IsecLS(Point a1, Point a2, Point b1, Point b2) {
return Cross(a2-a1, b1-a1) * Cross(a2-a1, b2-a1) < EPS;
}
// 線分と線分
bool IsecSS(Point a1, Point a2, Point b1, Point b2) {
return CCW(a1, a2, b1)*CCW(a1, a2, b2) <= 0 && CCW(b1, b2, a1)*CCW(b1, b2, a2) <= 0;
}
// 線分と点
bool IsecSP(Point a1, Point a2, Point b) {
return !CCW(a1, a2, b);
}
// ********************************************
// 距離 (Dist) ********************************
// 点pの直線aへの射影点を返す
Point Proj(Point a1, Point a2, Point p){
return a1 + Dot(a2-a1, p-a1) / norm(a2-a1) * (a2-a1);
}
// 点pの直線aへの反射点を返す
Point Reflection(Point a1, Point a2, Point p){
return 2.0L*Proj(a1, a2, p) - p;
}
// 直線と点
LD DistLP(Point a1, Point a2, Point p){
return abs(Proj(a1, a2, p) - p);
}
// 直線と直線
LD DistLL(Point a1, Point a2, Point b1, Point b2) {
return IsecLL(a1, a2, b1, b2) ? 0 : DistLP(a1, a2, b1);
}
// 直線と線分
LD DistLS(Point a1, Point a2, Point b1, Point b2) {
return IsecLS(a1, a2, b1, b2) ? 0 : min(DistLP(a1, a2, b1), DistLP(a1, a2, b2));
}
// 線分と点
LD DistSP(Point a1, Point a2, Point p) {
if(Dot(a2-a1,p-a1) < EPS) return abs(p-a1);
if(Dot(a1-a2,p-a2) < EPS) return abs(p-a2);
return abs(Cross(a2-a1,p-a1)) / abs(a2-a1);
}
// 線分と線分
LD DistSS(Point a1, Point a2, Point b1, Point b2) {
if(IsecSS(a1, a2, b1, b2)) return 0;
return min(min(DistSP(a1, a2, b1), DistSP(a1, a2, b2)), min(DistSP(b1, b2, a1), DistSP(b1, b2, a2)));
}
// ********************************************
// 2直線の交点 (CrossPoint) *******************
Point CrossPointLL(Point a1, Point a2, Point b1, Point b2){
LD d1 = Cross(b2-b1, b1-a1);
LD d2 = Cross(b2-b1, a2-a1);
if (EQ(d1, 0) && EQ(d2, 0)) return a1;
if (EQ(d2, 0)) throw "not exist crosspoint";
return a1 + d1/d2 * (a2-a1);
}
// ********************************************
//*********************************************
// 多角形(Polygon) *
//*********************************************
namespace std {
bool operator < (const Point &a, const Point &b){
return a.X != b.X ? a.X < b.X : a.Y < b.Y;
}
}
// 多角形の面積 (PolygonErea) *****************
LD PolygonErea(Polygon p){
LD area = 0;
int n = p.size();
for(int i = 0; i < n; i++){
area += Cross(p[i], p[(i+1) % n]);
}
return area / 2;
}
// ********************************************
// 凸性判定 (IsConvex) ************************
bool IsConvex(Polygon p){
int n = p.size();
for(int i = 0; i < n; i++){
if(CCW(p[i], p[(i+1) % n], p[(i+2) % n]) == -1) return false;
}
return true;
}
// ********************************************
/*************** using variables ***************/
Polygon p;
int n;
/**********************************************/
signed main(){
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n;
REP(i, n){
LD xp, yp;
cin >> xp >> yp;
p.pb(Point(xp, yp));
}
printf("%d\n", IsConvex(p));
}
|
#include <iostream>
#include <vector>
using namespace std;
template <class T> void readPoint(vector< vector<T> >& vec, int idx) {
T x, y;
vector<T> point(2);
std::cin >> x >> y;
point[0] = x;
point[1] = y;
vec[idx] = point;
}
int main()
{
int size;
cin >> size;
vector< vector<int> > points(size);
for (int i = 0; i < size; i++) {
readPoint(points, i);
}
int ans(1), dir(0), vecx(0), vecy(0), new_vecx(0), new_vecy(0);
for (int i = 0; i <= size; i++) {
new_vecx = points[(i + 1) % size][0] - points[i % size][0];
new_vecy = points[(i + 1) % size][1] - points[i % size][1];
int cross_prod = (vecx * new_vecy - vecy * new_vecx);
if (cross_prod != 0) {
if (cross_prod * dir >= 0) {
dir = cross_prod > 0 ? 1 : -1;
}
else {
ans = 0;
break;
}
}
vecx = new_vecx;
vecy = new_vecy;
}
cout << ans << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}
typedef long double D;
const D PI = acos(-1.0);
const D EPS = 1e-10;
class P { public:
D x, y;
P(D x=0, D y=0) : x(x), y(y) {}
P& operator+=(const P& o) { x += o.x; y += o.y; return *this; }
P& operator-=(const P& o) { x -= o.x; y -= o.y; return *this; }
P& operator*=(const P& o) { return *this = {x*o.x - y*o.y, x*o.y + y*o.x}; }
P& operator*=(const D& r) { x *= r; y *= r; return *this; }
P& operator/=(const D& r) { x /= r; y /= r; return *this; }
P operator-() const { return {-x, -y}; }
D norm() const { return x*x + y*y; }
D abs() const { return sqrt(norm()); }
D arg() const { return atan2(y, x); }
bool isZero() const { return std::abs(x) < EPS && std::abs(y) < EPS; }
/** 象限 */
int orth() const { return y >= 0 ? (x >= 0 ? 1 : 2) : (x < 0 ? 3 : 4); }
static P polar(const D& rho, const D& theta = 0) { return {rho * cos(theta), rho * sin(theta)}; }
};
std::ostream &operator<<(std::ostream &os, P const &p) { return os << "(" << p.x << ", " << p.y << ")"; }
std::istream &operator>>(std::istream &is, P &p) { D a, b; is >> a >> b; p = P(a, b); return is; }
P operator+(const P& p, const P& q) { return P(p) += q; }
P operator-(const P& p, const P& q) { return P(p) -= q; }
P operator*(const P& p, const P& q) { return P(p) *= q; }
P operator*(const P& p, const D& r) { return P(p) *= r; }
P operator/(const P& p, const D& r) { return P(p) /= r; }
P operator*(const D& r, const P& p) { return P(p) *= r; }
P operator/(const D& r, const P& p) { return P(p) /= r; }
D crs(const P& a, const P& b){ return a.x*b.y - a.y*b.x; }
D dot(const P& a, const P& b){ return a.x*b.x + a.y*b.y; }
int signum(D x) {return x > EPS ? +1 : x < -EPS ? -1 : 0;}
// 辞書順ソート
bool operator<(const P& a, const P& b) {
if (a.x != b.x) return a.x < b.x;
return a.y < b.y;
}
// // 偏角ソート
// bool operator<(const P& a, const P& b) {
// // atan2を使う方法。誤差に注意
// // return a.arg() < b.arg();
// // cosを使う方法。(0,0)の扱いに注意
// if (a.isZero() != b.isZero()) return a.isZero() > b.isZero();
// if (a.orth() != b.orth()) return a.orth() < b.orth();
// return crs(a, b) > 0;
// }
/** ベクトルpをベクトルbに射影したベクトル */
P proj(const P& p, const P& b) {
P t = b * dot(p, b);
return t / b.norm();
}
/** 点pから直線abに引いた垂線の足となる点 */
P footOfLP(const P& a, const P& b, const P& p) {
return a + proj(p-a, b-a);
}
/** 直線abを挟んで点pと対称な点 */
P reflection(const P&a, const P&b, const P& p) {
return 2 * footOfLP(a, b, p) - p;
}
int ccw(const P& a, P b, P c) {
// return signum(crs(b - a, c - a));
b -= a; c -= a;
if (crs(b, c) > 0) return +1; // counter clockwise
if (crs(b, c) < 0) return -1; // clockwise
if (dot(b, c) < 0) return +2; // c--a--b on line
if (b.norm() < c.norm()) return -2; // a--b--c on line
return 0;
}
/** 2直線の直行判定 : a⊥b <=> dot(a, b) = 0 */
bool isOrthogonal(const P& a1, const P& a2, const P& b1, const P& b2) {
return abs(dot(a1-a2, b1-b2)) < EPS;
}
/** 2直線の平行判定 : a//b <=> crs(a, b) = 0 */
bool isParallel(const P& a1, const P& a2, const P& b1, const P& b2) {
return abs(crs(a1-a2, b1-b2)) < EPS;
}
/** 点cが線分ab上にあるか : |a-c| + |c-b| <= |a-b| なら線分上 */
bool isIntersectSP(const P& a, const P& b, const P& c){
return ((a-c).abs() + (c-b).abs() < (a-b).abs() + EPS);
}
/** 直線aと直線bの交差判定 */
bool isIntersectLL(const P& a, const P& b, const P& c, const P& d){
return abs(crs(b-a, d-c)) > EPS || // non-parallel
abs(crs(b-a, c-a)) < EPS; // same line
}
/** 直線abと線分cdの交差判定 */
bool isIntersectLS(const P& a, const P& b, const P& c, const P& d){
return crs(b-a, c-a) * //c is left of ab
crs(b-a, d-a) < EPS; //d is right of ab
}
/** 線分と線分の交差判定。端点が重なってもtrue */
bool isIntersectSS(const P& a1, const P& a2, const P& b1, const P& b2){
return ccw(a1,a2,b1) * ccw(a1,a2,b2) <= 0 &&
ccw(b1,b2,a1) * ccw(b1,b2,a2) <= 0;
}
P intersectionLL(const P& a1, const P& a2, const P& b1, const P& b2){
P a = a2 - a1; P b = b2 - b1;
return a1 + a * crs(b, b1-a1) / crs(b, a);
}
D distSP(const P& a, const P& b, const P& c) {
if (dot(b-a, c-a) < EPS) return (c-a).abs(); // c a--b
if (dot(a-b, c-b) < EPS) return (c-b).abs(); // a--b c
return abs(crs(b-a, c-a)) / (b-a).abs();
}
D distSS(const P& a1, const P& a2, const P& b1, const P& b2){
if (isIntersectSS(a1,a2,b1,b2)) return 0.0;
return min(min(distSP(a1, a2, b1), distSP(a1, a2, b2)),
min(distSP(b1, b2, a1), distSP(b1, b2, a2)));
}
double area(const vector<P> &v) {
double ret = 0.0;
REP(i, v.size()) ret += crs(v[i], v[(i+1) % v.size()]);
return abs(ret / 2.0);
}
bool isConvex(const vector<P> &ps) {
int N = ps.size();
int m = 0;
REP(i, N) {
int dir = ccw(ps[i], ps[(i+1)%N], ps[(i+2)%N]);
if (dir == 1) m |= 1;
if (dir == -1) m |= 2;
}
return m != 3;
}
int main2() {
int N; cin >> N;
vector<P> ps(N);
REP(i, N) cin >> ps[i];
cout << (isConvex(ps) ? 1 : 0) << endl;
return 0;
}
int main() {
#ifdef LOCAL
for (;!cin.eof();cin>>ws)
#endif
main2();
return 0;
}
|
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <cmath>
#include <iomanip>
#include <complex>
using namespace std;
#define REP(i,n) for (int i=0;i<(n);++i)
#define rep(i,a,b) for(int i=a;i<(b);++i)
template<class T> inline bool chmin(T &a, T b){ if(a > b) { a = b; return true;} return false;}
template<class T> inline bool chmax(T &a, T b){ if(a < b) { a = b; return true;} return false;}
using ll = long long;
constexpr long long INF = 1LL << 62;
constexpr int MOD = 1e9 + 7;
constexpr double EPS = 1e-10;
using Point = complex<double>;
using Line = pair<Point, Point>;
using Polygon = vector<Point>;
bool compare(const Point &a, const Point &b) { // x ascending order
if(a.real() != b.real()) return a.real() < b.real();
return a.imag() < b.imag();
};
double dot(Point p, Point q) { return (conj(p) * q).real(); }
double cross(Point p, Point q) { return (conj(p) * q).imag(); }
double slope(Line l) { return tan(arg(l.second - l.first)); }
Point project(Line l, Point p) { // project p onto line (s,t)
return l.first + (l.second - l.first) * dot(p - l.first, l.second - l.first) / norm(l.second - l.first);
}
Point reflect(Line l, Point p) {
return l.first + conj((p - l.first) / (l.second - l.first)) * (l.second - l.first);
}
int ccw(Point a, Point b, Point c) {
b -= a; c -= a;
if(cross(b, c) > EPS) return +1; // counter-clockwise
if(cross(b, c) < -EPS) return -1; // clockwise
if( dot(b, c) < -EPS) return +2; // c--a--b
if(abs(b)+EPS < abs(c))return -2; // a--b--c
return 0; // a--c--b
}
bool intersect(Line a, Line b) {
return ccw(a.first, a.second, b.first) * ccw(a.first, a.second, b.second) <= 0 &&
ccw(b.first, b.second, a.first) * ccw(b.first, b.second, a.second) <= 0;
}
Point cross_point(Line a, Line b) {
Point base = b.second - b.first;
double area1 = abs(cross(base, a.first - b.first));
double area2 = abs(cross(base, a.second - b.first));
double t = area1 / (area1 + area2);
return a.first + (a.second - a.first) * t;
}
// dist line and p
double distance(Point p, Line l) { return abs(cross(l.second - l.first, p - l.first) / abs(l.second - l.first)); }
double distance(Line s, Point p) {
Point a = s.first, b = s.second;
if(dot(b - a, p - a) < EPS) return abs(p - a);
if(dot(a - b, p - b) < EPS) return abs(p - b);
return distance(p, s);
}
double distance(Line s1, Line s2) {
if(intersect(s1, s2)) return 0.0;
return min(min(distance(s1, s2.first), distance(s1, s2.second)), min(distance(s2, s1.first), distance(s2, s1.second)));
}
double area(Polygon &ps) {
double res = 0.0;
for(int i = 0; i < (int)ps.size(); ++i)
res += cross(ps[i], ps[(i+1)%(int)ps.size()]);
return abs(res) / 2.0;
}
Polygon convex_hull(Polygon &ps) {
sort(ps.begin(), ps.end(), compare);
int n = (int)ps.size(), k = 0;
Polygon res(n * 2);
// use "< -EPS" if include corner or boundary, otherwise, use "< EPS"
for(int i = 0; i < n; res[k++] = ps[i++])
while(k >= 2 && cross(res[k-1] - res[k-2], ps[i] - res[k-1]) < EPS) --k;
for(int i = n - 2, t = k+1; i >= 0; res[k++] = ps[i--])
while(k >= t && cross(res[k-1] - res[k-2], ps[i] - res[k-1]) < EPS) --k;
res.resize(k - 1);
return res;
}
double caliper(Polygon &ps) {
ps = convex_hull(ps);
int n = (int)ps.size();
if(n == 2) {return abs(ps[0] - ps[1]);}
int i = 0, j = 0;
// j --> (x asc order) --> i
for(int k=0; k < n; ++k) {
if(compare(ps[i],ps[k])) i = k;
if(compare(ps[k],ps[j])) j = k;
}
int si = i, sj = j;
double res = 0.0;
// rotate 180 degrees
while(i != sj || j != si) {
res = max(res, abs(ps[i] - ps[j]));
if(cross(ps[(i + 1) % n] - ps[i], ps[(j + 1) % n] - ps[j]) < 0) {
i = (i + 1) % n;
} else {
j = (j + 1) % n;
}
}
return res;
}
int main() {
cin.tie(0); ios_base::sync_with_stdio(false);
cout << fixed << setprecision(1);
int q; cin >> q;
Polygon ps;
for(int i=0; i < q; ++i) {
double x,y; cin >> x >> y;
ps.emplace_back(x,y);
}
double res = 0.0;
for(int i=0; i < q; ++i) {
double tmp = cross(ps[(i+2)%q]-ps[(i+1)%q], ps[(i+1)%q]-ps[i]);
if(res * tmp < 0) {
cout << 0 << endl;
return 0;
}
res = tmp;
}
cout << 1 << endl;
return 0;
}
|
#include <iostream>
#include <complex>
#include <vector>
using namespace std;
double cross(complex< double > a, complex< double > b){
return a.real() * b.imag() - a.imag() * b.real();
}
int main() {
int n,x,y,convex;
vector< complex< double > > vertex;
cin >> n;
vertex.resize(n);
for(int i=0;i<n;++i){
cin >> x >> y;
vertex[i] = complex< double >(x, y);
}
convex = 1;
for(int i=0;i<n;++i){
if(cross(vertex[(i+1)%n]-vertex[i], vertex[(i+2)%n]-vertex[(i+1)%n]) < 0){
convex = 0;
break;
}
}
cout << (convex ? 1 : 0) << endl;
return 0;
} |
#include<bits/stdc++.h>
#define Re real()
#define Im imag()
using namespace std;
const double eps = 1e-9;
typedef complex<double> Point;
typedef Point Vector;
Point P[50050];
int n, k;
vector<Point> qs;
bool cmp(Point a, Point b)
{
if(fabs(a.Re - b.Re) < eps) return a.Im < b.Im;
else return a.Re < b.Re;
}
double cross(const Point& a, const Point& b)
{
return (conj(a) * b).Im;
}
int convex_hull()
{
k = 0;
for(int i = 0; i < n; i++)
{
while(k > 1 && cross(qs[k - 1] - qs[k - 2], P[i] - qs[k - 1]) <= 0) k--;
qs[k++] = P[i];
}
for(int i = n - 2, t = k; i >= 0; i--)
{
while(k > t && cross(qs[k - 1] - qs[k - 2], P[i] - qs[k - 1]) <= 0) k--;
qs[k++] = P[i];
}
return k - 1;
}
int main()
{
while(~scanf("%d", &n))
{
for(int i = 0; i < n; i++)
{
double tx, ty;
scanf("%lf%lf", &tx, &ty);
P[i] = Point(tx, ty);
}
int flag = 1;
for(int i = 0; i < n; i++)
{
Vector v1 = P[(i - 1 + n) % n] - P[i];
Vector v2 = P[(i + 1) % n] - P[i];
if(cross(v1, v2) > 0) flag = 0;
}
printf("%d\n",flag);
}
return 0;
}
/*
5
0 0
2 0
2 2
0 2
1 1
*/ |
#include<bits/stdc++.h>
using namespace std;
// BEGIN CUT HERE
namespace geometry {
using Real = double;
using Point = complex<Real>;
constexpr Real EPS = 1e-9;
// 誤差を考慮した符号
inline int sgn(const Real x) {
return (x > EPS) - (x < -EPS);
}
// 誤差を考慮した l - r の符号
inline int sgn(const Real l, const Real r) {
return sgn(l - r);
}
struct Circle {
Point p;
Real r;
Circle(const Point p, const Real r) : p(p), r(r) {}
};
struct Line {
Point a, b;
Line(const Point a, const Point b) : a(a), b(b) {}
};
struct Segment : Line {
Segment(const Point a, const Point b) : Line(a, b) {}
};
// 内積
inline Real dot(const Point& lhs, const Point& rhs) {
return (conj(lhs) * rhs).real();
}
// 外積
inline Real cross(const Point& lhs, const Point& rhs) {
return (conj(lhs) * rhs).imag();
}
// a の b に対する正射影ベクトル
inline Point projection(const Point& a, const Point& b) {
return b * dot(a, b) / norm(b);
}
// 点pから直線lに下ろした垂線の足
inline Point projection(const Point& p, const Line& l) {
return projection(p - l.a, l.b - l.a) + l.a;
}
// 直線lを対称軸として点pと線対称な点
inline Point reflection(const Point &p, const Line &l) {
return p + (projection(p, l) - p) * 2.0;
}
// 反時計回り(a -> b -> c)
static constexpr int CCW_COUNTER_CLOCKWISE = 0b00001;
// 時計回り(a -> b -> c)
static constexpr int CCW_CLOCKWISE = 0b00010;
// 同一直線上(c -> a -> b)
static constexpr int CCW_ONLINE_BACK = 0b00100;
// 同一直線上(a -> b -> c, b on ac)
static constexpr int CCW_ONLINE_FRONT = 0b01000;
// 同一直線上(a -> c -> b)
static constexpr int CCW_ON_SEGMENT = 0b10000;
// 3点の位置関係
inline int ccw(const Point &a, Point b, Point c) {
b = b - a, c = c - a;
if(cross(b, c) > EPS) return CCW_COUNTER_CLOCKWISE;
if(cross(b, c) < -EPS) return CCW_CLOCKWISE;
if(dot(b, c) < 0) return CCW_ONLINE_BACK;
if(norm(b) < norm(c)) return CCW_ONLINE_FRONT;
return CCW_ON_SEGMENT;
}
// 平行判定
inline bool parallel(const Line& l1, const Line& l2) {
return sgn(cross(l1.a - l1.b, l2.a - l2.b)) == 0;
}
// 直交判定
inline bool orthogonal(const Line& l1, const Line& l2) {
return sgn(dot(l1.a - l1.b, l2.a - l2.b)) == 0;
}
// 直線と直線の交差判定
inline bool intersect(const Line& l1, const Line& l2) {
return not parallel(l1, l2);
}
// 線分と直線の交差判定
inline bool intersect(const Segment &s, const Line &l) {
constexpr int plus = CCW_COUNTER_CLOCKWISE | CCW_ONLINE_BACK;
constexpr int minus = CCW_CLOCKWISE | CCW_ONLINE_FRONT;
int f[2] = {ccw(l.a, l.b, s.a), ccw(l.a, l.b, s.b)};
for(int i = 0; i < 2; ++i) {
f[i] = (f[i] & plus) ? +1 : ((f[i] & minus) ? -1 : 0);
}
return (f[0] * f[1] <= 0);
}
// 線分と線分の交差判定
inline bool intersect(const Segment& s1, const Segment& s2) {
constexpr int plus = CCW_COUNTER_CLOCKWISE | CCW_ONLINE_BACK;
constexpr int minus = CCW_CLOCKWISE | CCW_ONLINE_FRONT;
int f[4] = {ccw(s1.a, s1.b, s2.a), ccw(s1.a, s1.b, s2.b), ccw(s2.a, s2.b, s1.a), ccw(s2.a, s2.b, s1.b)};
for(int i = 0; i < 4; ++i) {
f[i] = (f[i] & plus) ? +1 : ((f[i] & minus) ? -1 : 0);
}
return (f[0] * f[1] <= 0) and (f[2] * f[3] <= 0);
}
// 直線と直線の交点(要:交差判定)
inline Point crossPoint(const Line& l1, const Line& l2) {
assert(intersect(l1, l2));
Point a = l1.b - l1.a;
Point b = l2.b - l2.a;
return l1.a + a * cross(b, l2.a - l1.a) / cross(b, a);
}
// 線分と線分の交点(要:交差判定)
inline Point crossPoint(const Segment& s1, const Segment& s2) {
assert(intersect(s1, s2));
return crossPoint((Line)s1, (Line)s2);
}
// 円と円の交点
vector<Point> crossPoint(const Circle& c1, const Circle& c2) {
const Real dist = abs(c1.p - c2.p);
vector<Point> ret;
if(dist > c1.r + c2.r) return ret;
if(dist < abs(c1.r - c2.r)) return ret;
const Real rc = (dist * dist + c1.r * c1.r - c2.r * c2.r) / (2 * dist);
const Real rs = sqrt(c1.r * c1.r - rc * rc);
const Point vec = (c2.p - c1.p) / dist;
ret.emplace_back(c1.p + vec * Point(rc, rs));
ret.emplace_back(c1.p + vec * Point(rc, -rs));
return ret;
}
// 点と点の距離
inline Real distance(const Point &a, const Point &b) {
return abs(a - b);
}
// 点と直線の距離
inline Real distance(const Point &p, const Line &l) {
return abs(p - projection(p, l));
}
// 点と線分の距離
inline Real distance(const Point &p, const Segment &s) {
const Point pro = projection(p, s);
const Line l(p, pro);
if(intersect(s, l)) return distance(p, pro);
return min(distance(p, s.a), distance(p, s.b));
}
// 線分と直線の距離
inline Real distance(const Segment &s, const Line &l) {
if(intersect(s, l)) return 0.0;
Real ret = distance(s.a, l);
ret = min(ret, distance(s.b, l));
return ret;
}
// 線分と線分の距離
inline Real distance(const Segment &s1, const Segment &s2) {
if(intersect(s1, s2)) return 0.0;
Real ret = distance(s1.a, s2);
ret = min(ret, distance(s1.b, s2));
ret = min(ret, distance(s2.a, s1));
ret = min(ret, distance(s2.b, s1));
return ret;
}
// 面積
inline Real area(const vector<Point>& p) {
int n = p.size();
Real ret = 0;
for(int i = 1; i <= n; ++i) {
ret += cross(p[i - 1], p[i % n]);
}
return abs(ret / 2);
}
// 凸性判定(if rev false : 反時計回り)
inline bool convexity(const vector<Point>& p, const bool rev = false) {
int n = p.size();
int ret = 0;
for(int i = 1; i <= n; ++i) {
ret |= ccw(p[(i - 1) % n], p[i % n], p[(i + 1) % n]);
}
if(rev) {
ret &= CCW_COUNTER_CLOCKWISE;
} else {
ret &= CCW_CLOCKWISE;
}
return ret == 0;
}
inline istream& operator>>(istream& is, Point& p) {
Real x, y;
is >> x >> y;
p = Point(x, y);
return is;
}
inline ostream& operator<<(ostream& os, const Point &p) {
return os << fixed << setprecision(15) << p.real() << " " << p.imag();
}
}
// END CUT HERE
using namespace geometry;
int main() {
int n; cin >> n;
vector<Point> p(n);
for(int i = 0; i < n; ++i) cin >> p[i];
cout << convexity(p) << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template<class T>
struct Point {
typedef Point P;
T x, y;
explicit Point(T x=0, T y=0) : x(x), y(y) {}
bool operator<(P p) const { return tie(x,y) < tie(p.x,p.y); }
bool operator==(P p) const { return tie(x,y)==tie(p.x,p.y); }
P operator+(P p) const { return P(x+p.x, y+p.y); }
P operator-(P p) const { return P(x-p.x, y-p.y); }
P operator*(T d) const { return P(x*d, y*d); }
P operator/(T d) const { return P(x/d, y/d); }
T dot(P p) const { return x*p.x + y*p.y; }
T cross(P p) const { return x*p.y - y*p.x; }
T cross(P a, P b) const { return (a-*this).cross(b-*this); }
T dist2() const { return x*x + y*y; }
double dist() const { return sqrt((double)dist2()); }
// angle to x-axis in interval [-pi, pi]
double angle() const { return atan2(y, x); }
P unit() const { return *this/dist(); } // makes dist()=1
P perp() const { return P(-y, x); } // rotates +90 degrees
P normal() const { return perp().unit(); }
// returns point rotated 'a' radians ccw around the origin
P rotate(double a) const {
return P(x*cos(a)-y*sin(a),x*sin(a)+y*cos(a)); }
};
using P = Point<long long>;
template<class T>
T polygonArea2(vector<Point<T>>& v) {
T a = v.back().cross(v[0]);
rep(i,0,sz(v)-1) a += v[i].cross(v[i+1]);
return a;
}
int main(){
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cout << fixed << setprecision(15);
int n;
cin >> n;
vector<P> a(n);
for(int i = 0; i < n; i++){
cin >> a[i].x >> a[i].y;
}
for(int i = 0; i < n; i++){
P r = a[(i+2) % n] - a[(i+1) % n];
P s = a[i] - a[(i+1) % n];
if(r.cross(s) < 0){
cout << 0 << '\n';
exit(0);
}
}
cout << 1 << '\n';
}
|
#include<bits/stdc++.h>
using namespace std;
typedef complex<double> P;
typedef vector<P> vec;
double eps=1e-7;
bool eq(double a,double b){return (b-a<eps&&a-b<eps);}
double dot(P a,P b){return real(b*conj(a));}
double cross(P a,P b){return imag(b*conj(a));}
P project(P a,P b,P c){b-=a;c-=a;return a+b*real(c/b);}
P reflect(P a,P b,P c){b-=a;c-=a;return a+conj(c/b)*b;}
int ccw(P a,P b,P c){
b-=a,c-=a,a=c*conj(b);
if(a.imag()>eps)return 1;//ccw
if(a.imag()<-eps)return -1;//cw
if(a.real()<-eps)return 2;//ob
if(abs(b)+eps<abs(c))return -2;//of
return 0;//os
}
//segment ab , point c
double dist(P a,P b,P c){
if(dot(b-a,c-a)<0)return abs(c-a);
if(dot(a-b,c-b)<0)return abs(c-b);
return abs(cross(b-a,c-a))/abs(b-a);
}
bool isintersect(P a,P b,P c,P d){
return ((ccw(a,b,c)*ccw(a,b,d)<=0)&&(ccw(c,d,a)*ccw(c,d,b)<=0));
}
//segment ab , segment cd
P intersect(P a,P b,P c,P d){
a-=d;b-=d;c-=d;
return d+a+(b-a)*imag(a/c)/imag(a/c-b/c);
}
double dist(P a,P b,P c,P d){
if(isintersect(a,b,c,d))return 0;
double ab=min(dist(a,b,c),dist(a,b,d));
double cd=min(dist(c,d,a),dist(c,d,b));
return min(ab,cd);
}
double calcArea(vec &t){
double res=0;
int n=t.size();
for(int i=0;i<n;i++)res+=cross(t[i],t[(i+1==n?0:i+1)]);
return abs(res/2.0);
}
int main(){
int n;
cin>>n;
vec t;
double x,y;
for(int i=0;i<n;i++){
cin>>x>>y;
t.push_back(P(x,y));
}
int ans=1;
for(int i=0;i<n;i++){
int j=(i+1)%n;
int k=(i+2)%n;
if( ccw(t[i],t[j],t[k]) == -1 )ans=0;
}
cout<<ans<<endl;
return 0;
} |
#include <iostream>
#include <vector>
using namespace std;
template <class T> void readPoint(vector< vector<T> >& vec, int idx) {
T x, y;
vector<T> point(2);
std::cin >> x >> y;
point[0] = x;
point[1] = y;
vec[idx] = point;
}
int main()
{
int size;
cin >> size;
vector< vector<int> > points(size);
//vector<int> y(size);
for (int i = 0; i < size; i++) {
readPoint(points, i);
//readVal(y, i);
}
int ans(1), dir(0), vecx(0), vecy(0), new_vecx(0), new_vecy(0);
for (int i = 0; i <= size; i++) {
new_vecx = points[(i + 1) % size][0] - points[i % size][0];
new_vecy = points[(i + 1) % size][1] - points[i % size][1];
int cross_prod = (vecx * new_vecy - vecy * new_vecx);
if (cross_prod != 0) {
if (cross_prod * dir >= 0) {
dir = cross_prod > 0 ? 1 : -1;
}
else {
ans = 0;
break;
}
}
vecx = new_vecx;
vecy = new_vecy;
}
cout << ans << "\n";
return 0;
}
|
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
#define db double
#define zero(x) (fabs(x)<eps)
#define sgn(x) (zero(x)?0:((x)>0?1:-1))
const db eps=1e-10;
struct point{
db x,y;
point(){}
point(db x,db y):x(x),y(y){}
point operator + (point B){ return point(x+B.x,y+B.y); }
point operator - (point B){ return point(x-B.x,y-B.y); }
point operator * (db k){ return point(k*x,k*y); }
db operator * (point B){ return x*B.y-y*B.x; }
db operator ^ (point B){ return x*B.x+y*B.y; }
bool operator < (point B){ return (y>0)!=(B.y>0)?y>0:((*this)*B)>0; }
};
db dis2(point A,point B){ return (A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y); }
struct line{
point A,B;
line(){}
line(point A,point B):A(A),B(B){}
};
point projection(line l,point P){
point dir=l.B-l.A; P=P-l.A;
return dir*((P^dir)/dis2(l.A,l.B))+l.A;
}
point reflection(line l,point P){
return projection(l,P)*2-P;
}
int direction(point P0,point P1,point P2){
db det=(P2-P0)*(P1-P0); return sgn(det);
}
bool parallel(line l1,line l2){
return zero((l1.B-l1.A)*(l2.B-l2.A));
}
bool orthogonal(line l1,line l2){
return zero((l1.B-l1.A)^(l2.B-l2.A));
}
bool intersect(line l1,line l2){//
int d1=direction(l1.A,l1.B,l2.A),d2=direction(l1.A,l1.B,l2.B);
int d3=direction(l2.A,l2.B,l1.A),d4=direction(l2.A,l2.B,l1.B);
if (d1*d2>0||d3*d4>0) return false;
if (l1.A.x>l1.B.x) swap(l1.A.x,l1.B.x);
if (l1.A.y>l1.B.y) swap(l1.A.y,l1.B.y);
if (l2.A.x>l2.B.x) swap(l2.A.x,l2.B.x);
if (l2.A.y>l2.B.y) swap(l2.A.y,l2.B.y);
return !(l1.B.x<l2.A.x||l2.B.x<l1.A.x||l1.B.y<l2.A.y||l2.B.y<l1.A.y);
}
point cross_point(line l1,line l2){
db s1=fabs((l2.B-l2.A)*(l1.A-l2.A)),s2=fabs((l2.B-l2.A)*(l1.B-l2.A));
return l1.A+(l1.B-l1.A)*(s1/(s1+s2));
}
db dis(line l,point P){//
point proj=projection(l,P);
if (min(l.A.x,l.B.x)-eps<proj.x&&proj.x<max(l.A.x,l.B.x)+eps)
if (min(l.A.y,l.B.y)-eps<proj.y&&proj.y<max(l.A.y,l.B.y)+eps)
return sqrt(dis2(P,proj));
return sqrt(min(dis2(P,l.A),dis2(P,l.B)));
}
db distance(line l1,line l2){
if (intersect(l1,l2)) return 0;
return min(min(dis(l1,l2.A),dis(l1,l2.B)),min(dis(l2,l1.A),dis(l2,l1.B)));
}
db area(point *a,int n){
db ans=0; for (int i=2;i<=n-1;++i) ans+=(a[i]-a[1])*(a[i+1]-a[1])/2;
return ans;
}
bool check(point *a,int n){
int t=sgn((a[2]-a[1])*(a[n]-a[1])),cnt1=0,cnt2=0;
if (t>0)++cnt1;
else if (t<0) ++cnt2;
for (int i=2;i<=n;++i){
t=sgn((a[i%n+1]-a[i])*(a[i-1]-a[i]));
if (t>0) ++cnt1;
else if (t<0) ++cnt2;
if (cnt1&&cnt2) return false;
}
return true;
}
const int N=1e6+6;
point A[N];
int main(){
int n; scanf("%d",&n);
for (int i=1;i<=n;++i) scanf("%lf%lf",&A[i].x,&A[i].y);
printf("%d\n",check(A,n));
}
|
#include <iostream>
#include <algorithm>
#include <iterator>
#include <iomanip>
#include <cmath>
#include <vector>
#include <numeric>
#include <cstdio>
#include <bitset>
#include <map>
#include <string>
#include <valarray>
#include <queue>
#include <utility>
#include <functional>
#include <list>
#include <cmath>
using namespace std;
const double ESP = 1E-8;
struct Point
{
//friends
friend Point operator*(double c, const Point & p);
friend ostream & operator<<(ostream & os, const Point & p);
friend istream & operator>>(istream & is, Point & p);
friend double cross(const Point & p1, const Point & p2);
//data members
double x, y;
//function members
Point() {};
Point(double x, double y): x(x), y(y) {};
Point operator-(const Point & p) const {return Point(x - p.x, y - p.y);}
double operator*(const Point & p) const {return x*p.x + y*p.y;}
Point operator*(double c) const {return Point(c*x, c*y);}
Point operator+(const Point & p) const {return Point(x+p.x, y+p.y);}
Point projection(const Point & p1, const Point & p2) const {Point a(*this-p1), b(p2-p1); return (a*b)/(b*b)*b+p1;}
Point reflection(const Point & p1, const Point & p2) const {return this->projection(p1, p2)*2 - *this;}
};
struct Line
{
Line() = default;
Line(const Point & p1, const Point & p2):
direction(p1-p2), point_online(p1) {};
Point direction, point_online;
Point get_normal() const {return Point(direction.y, -direction.x);}
};
class Polygon
{
friend istream & operator>>(istream & is, Polygon & p);
public:
double area();
bool is_convex();
private:
vector<Point> Vertices;
};
int main()
{
Polygon polygon;
cin >> polygon;
cout << polygon.is_convex() << endl;
}
//function members and friend functions of Point
Point operator*(double c, const Point & p)
{
return p*c;
}
ostream & operator<<(ostream & os, const Point & p)
{
os << setprecision(8) << fixed << p.x << ' ' << p.y << endl;
return os;
}
istream & operator>>(istream & is, Point & p)
{
is >> p.x >> p.y;
return is;
}
double cross(const Point & p1, const Point & p2)
{
return p1.x*p2.y-p1.y*p2.x;
}
//function members and friend functions of Polygon
istream & operator>>(istream & is, Polygon & p)
{
int q;
is >> q;
p.Vertices.resize(q);
for (int i = 0; i < q; ++i)
{
cin >> p.Vertices[i];
}
return is;
}
double Polygon::area()
{
double a = 0;
for (int i = 0; i < Vertices.size(); ++i)
{
a += cross(Vertices[i], Vertices[(i+1)%Vertices.size()]);
}
return fabs(a/2);
}
bool Polygon::is_convex()
{
int ans = 0;
for (int i = 0; i < Vertices.size()-1; ++i)
{
double tmp = cross(Vertices[(i+1)%Vertices.size()]-Vertices[i], Vertices[(i+2)%Vertices.size()]-Vertices[i]);
if (tmp == 0)
continue;
if (ans == 0)
{
if (tmp < 0)
ans = -1;
else
ans = 1;
continue;
}
if (ans * tmp < 0)
return false;
}
return true;
}
|
#include<bits/stdc++.h>
#define REP(i,first,last) for(int i=first;i<=last;++i)
#define DOW(i,first,last) for(int i=first;i>=last;--i)
using namespace std;
const double PI=3.14159265358979323846264338327950288419716939;
struct Point
{
double x,y;
Point(double fx=0,double fy=0)
{
x=fx;
y=fy;
}
void Read()
{
scanf("%lf%lf",&x,&y);
}
void Write()
{
printf("%.9lf %.9lf",x,y);
}
double operator ^(Point const b)const
{
return x*b.x+y*b.y;
}
double operator |(Point const b)const
{
return x*b.y-b.x*y;
}
double operator ==(Point const b)const
{
return sqrt((x-b.x)*(x-b.x)+(y-b.y)*(y-b.y));
}
Point operator *(double const b)const
{
return Point(x*b,y*b);
}
Point operator +(Point const b)const
{
return Point(x+b.x,y+b.y);
}
Point operator -(Point const b)const
{
return Point(x-b.x,y-b.y);
}
};
Point Symmetrical(Point a,Point m=Point(0,0))
{
return Point(m.x*2-a.x,m.y*2-a.y);
}
/**
* /
* a /
* m
* / b
* /
*
* a
*
* ---m-----
*
* b
* return b;
*/
struct Vector
{
Point a,b;
Vector(Point f=Point(0,0),Point e=Point(0,0))
{
a=f;
b=e;
}
double operator ^(Vector const c)const
{
return (a.x-b.x)*(c.a.x-c.b.x)+(c.a.y-c.b.y)*(a.y-b.y);
}
double operator |(Vector const c)const
{
return (a.x-b.x)*(c.a.y-c.b.y)-(c.a.x-c.b.x)*(a.y-b.y);
}
};
Point ProjectivePoint(Point a,Vector b)
{
double c=Vector(b.a,a)^Vector(b.a,b.b);
c/=(b.a==b.b)*(b.a==b.b);
return (b.b-b.a)*c+b.a;
}
/**
* *
* |
* |
* *---m-----*
*
* *
* |
* |
* *----* m
* return m;
*/
double PointToVector(Point a,Vector b)
{
Point c=ProjectivePoint(a,b);
if(
(
(b.a.x<=c.x)==(c.x<=b.b.x)
)
&&
(
(b.a.y<=c.y)==(c.y<=b.b.y)
)
)
{
return a==c;
}
return min(a==b.a,a==b.b);
}
/**
* *
* |
* *------*
*
* *
* /
* *------*
* return min_dis;
*/
bool InRectangle(Point a,Point b,Point c)
{
return
min(b.x,c.x)<=a.x&&a.x<=max(b.x,c.x)
&&
min(b.y,c.y)<=a.y&&a.y<=max(b.y,c.y);
}
/**
* a
* b---*
* | |
* *---c
* return 0;
*
* b---*
* |a |
* *---c
* return 1;
*/
bool RectangleIntersection(Point a0,Point a1,Point b0,Point b1)
{
int zx=fabs(a0.x+a1.x-b0.x-b1.x);
int zy=fabs(a0.y+a1.y-b0.y-b1.y);
int x=fabs(a0.x-a1.x)+fabs(b0.x-b1.x);
int y=fabs(a0.y-a1.y)+fabs(b0.y-b1.y);
if(zx<=x&&zy<=y)
{
return 1;
}
return 0;
}
/**
* *---*
* | |
* *---*
*
* *--*
* | |
* | |
* *--*
* return 0;
*
* *---*
* | *-+*
* *-+-*|
* | |
* *--*
* return 1;
*/
bool Intersect(Vector a,Vector b)
{
double a_aa=a|Vector(a.a,b.a);
double a_ab=a|Vector(a.a,b.b);
double b_aa=b|Vector(b.a,a.a);
double b_ab=b|Vector(b.a,a.b);
if((a_aa==0||a_ab==0||((a_aa<0)^(a_ab<0)))&&(b_aa==0||b_ab==0||((b_aa<0)^(b_ab<0))))
{
return RectangleIntersection(a.a,a.b,b.a,b.b);
}
return 0;
}
/**
* *
* /
* /*
* / \
* / \
* * *
* return 0;
*
* *
* * /
* \/
* /\
* / \
* * *
* return 1;
*/
Point QueryIntersect(Vector a,Vector b)
{
Vector u(a.a,b.a);
Vector v(b.a,b.b);
Vector w(a.a,a.b);
double c=(w|u)/(v|w);
return Point(b.a.x+(b.b.x-b.a.x)*c,b.a.y+(b.b.y-b.a.y)*c);
}
/**
* *
* * /
* \ /
* \ /
* m
* / \
* * \
* *
* return m;
*/
double VectorToVector(Vector a,Vector b)
{
if(Intersect(a,b))
{
return 0.0;
}
return min
(
min(PointToVector(a.a,b),PointToVector(a.b,b)),
min(PointToVector(b.a,a),PointToVector(b.b,a))
);
}
/**
* *----*
* |
* *--------*
*
* *-----*
* \
* *-----*
* return min_dis;
*/
double Area(Point point[],int n)
{
double result=0;
REP(i,2,n)
{
result+=point[i-1]|point[i];
}
result+=point[n]|point[1];
return fabs(result)/2.0;
}
double Perimeter(Point point[],int n)
{
double result=0;
REP(i,2,n)
{
result+=(point[i-1]==point[i]);
}
return result+(point[1]==point[n]);
}
bool CheckConvexHull(Point point[],int n)
{
double first=0,c;
point[++n]=point[1];
REP(i,1,n-1)
{
if(!first)
{
first=Vector(point[i],point[i+1])|Vector(point[i+1],point[i+2]);
}
else
{
c=(Vector(point[i],point[i+1])|Vector(point[i+1],point[i+2]));
if(c!=0&&((first<0)^(c<0)))
{
return 0;
}
}
}
return 1;
}
Point f_point;
bool Cmp(Point a,Point b)
{
double c=Vector(f_point,a)|Vector(f_point,b);
if(0<c)
{
return 1;
}
if(c==0&&(f_point==a)<(f_point==b))
{
return 1;
}
return 0;
}
int GetConvexHull(Point point[],Point st[],int n)
{
REP(i,2,n)
{
if(point[i].y<point[1].y||point[i].y==point[1].y&&point[i].x<point[1].x)
{
swap(point[i],point[1]);
}
}
f_point=point[1];
sort(point+2,point+1+n,Cmp);
int top=1;
st[1]=point[1];
REP(i,2,n)
{
while(1<top&&(Vector(st[top-1],st[top])|Vector(st[top],point[i]))<=0)
{
--top;
}
st[++top]=point[i];
}
return top;
}
double GetDiam(Point point[],int n)
{
double result=0;
if(n==2)
{
return point[1]==point[2];
}
point[++n]=point[1];
int top=2;
REP(i,1,n)
{
while(
(Vector(point[i],point[top])|Vector(point[top],point[i+1]))
>
(Vector(point[i],point[top+1])|Vector(point[top+1],point[i+1]))
)
{
top++;
if(top==n+1)
{
top=1;
}
}
result=max(result,max(point[i]==point[top],point[i+1]==point[top]));
}
return result;
}
const int MAXN=1e5+5;
int n;
Point p[MAXN];
int main()
{
scanf("%d",&n);
REP(i,1,n)
{
p[i].Read();
}
printf("%d\n",CheckConvexHull(p,n));
return 0;
}
|
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<list>
#include<string>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<ctime>
using namespace std;
typedef long long ll;
bool debug = false;
const int NIL = -1;
const int INF = 1000000000;
const int NUM = 100010;
const double eps = 1e-10;
clock_t START, END;
//basic defitition
struct Point {
double x, y;
Point(double x = 0, double y = 0) :x(x), y(y) {}
};
typedef Point Vector;
Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
Vector operator - (Point A, Point B) { return Vector(A.x - B.x, A.y - B.y); }
Vector operator * (Vector A, double p) { return Vector(A.x * p, A.y * p); }
Vector operator / (Vector A, double p) { return Vector(A.x / p, A.y / p); }
bool operator < (const Point& a, const Point& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }
int dcmp(double x) { if (fabs(x) < eps)return 0; else return x < 0 ? -1 : 1; }
bool operator == (const Point& a, const Point& b) { return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0; }
// basic operator
double Dot(Vector A, Vector B) { return A.x * B.x + A.y * B.y; }
double Length(Vector A) { return sqrt(Dot(A, A)); }
double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); }
double Cross(Vector A, Vector B) { return A.x * B.y - A.y * B.x; }
//point and line
Point GetLineIntersection(Point P, Vector v, Point Q, Vector w) {
Vector u = P - Q;
double t = Cross(w, u) / Cross(v, w);
return P + v * t;
}//two lines only have one intersection and Cross(v,w) is not zero
double ConvexPolygonArea(vector<Point>& p, int n) {
double area = 0;
for (int i = 1; i < n; i++) {
area += Cross(p[i] - p[0], p[i + 1] - p[0]);
}
return area / 2;
}
int main(void)
{
if (debug) {
START = clock();
freopen("in29.txt", "r", stdin);
freopen("out.txt", "w", stdout);
};
bool ok = true;
int n;
double x, y;
vector<Point> p;
cin >> n;
for (int i = 0; i < n; i++) {
scanf("%lf%lf", &x, &y);
p.push_back(Point(x, y));
}
p.push_back(p[0]);
p.push_back(p[1]);
for (int i = 1; i <= n; i++) {
if (dcmp(Cross(p[i + 1] - p[i], p[i - 1] - p[i])) < 0) {
ok = false;
break;
}
}
printf("%d\n", ok);
if (debug) {
END = clock();
double endtime = (double)(END - START) / 1000;
printf("total time = %lf s", endtime);
}
return 0;
}
|
#include <iostream>
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <map>
#include <queue>
#include <set>
#include <tuple>
using namespace std;
typedef long long ll;
#define _ << " " <<
#define all(X) (X).begin(), (X).end()
#define len(X) (X).size()
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define Tiii tuple<int, int, int>
#define Tlll tuple<ll, ll, ll>
double eps = 1e-8;
struct point /* vec */ {
double x, y;
point operator+(const point &p) {
return {x + p.x, y + p.y};
}
point operator-(const point &p) {
return {x - p.x, y - p.y};
}
};
struct line {
// ax + by + c = 0
double a, b, c;
};
struct circle {
point p;
double r;
};
double dot(point P, point Q) {
return P.x*Q.x + P.y*Q.y;
}
double cross_2d(point P, point Q) {
return P.x*Q.y - P.y*Q.x;
}
double points_distance(point A, point B, int deg) {
point C = B - A;
return pow(C.x*C.x + C.y*C.y, 0.5 * deg);
}
line line_2points(point P, point Q) {
return {P.y - Q.y, Q.x - P.x, P.x*Q.y - P.y*Q.x};
}
point lines_intersection(line l, line m) {
if (abs(l.a*m.b - l.b*m.a) < eps) {
assert(-1);
}
return {- (l.b*m.c - l.c*m.b) / (l.b*m.a - l.a*m.b),
- (l.a*m.c - l.c*m.a) / (l.a*m.b - l.b*m.a)};
}
bool line_same(line l, line m) {
if (l.c * m.c == 0) {
if (l.c == m.c) {
if (l.b * m.b == 0) {
if (l.b == m.b) return 1;
else return 0;
}
else return l.a / l.b == m.a / m.b;
}
else return 0;
}
else return l.a / l.c == m.a / m.c && l.b / l.c == m.b / m.c;
}
bool on_line(point P, point Q, point R) {
if (P.x > Q.x) swap(P, Q);
if (P.y > Q.y) swap(P.y, Q.y);
return (P.x <= R.x && R.x <= Q.x && P.y <= R.y && R.y <= Q.y);
}
double point_line_distance(point P, line l) {
return abs(l.a * P.x + l.b * P.y + l.c) / sqrt(l.a*l.a + l.b*l.b);
}
double point_segment_distance(point p1, point p2, point q) {
if (dot(q - p1, p2 - p1) <= 0) return points_distance(p1, q, 1);
else if (dot(q - p2, p1 - p2) <= 0) return points_distance(p2, q, 1);
else return point_line_distance(q, line_2points(p1, p2));
}
double is_segments_intersect(point p1, point p2, point q1, point q2) {
line s0 = line_2points(p1, p2);
line s1 = line_2points(q1, q2);
if (abs(s0.a*s1.b - s0.b*s1.a) < eps) {
if (p1.x > p2.x) swap(p1, p2);
if (p1.y > p2.y) swap(p1.y, p2.y);
if (q1.x > q2.x) swap(q1, q2);
if (q1.y > q2.y) swap(q1.y, q2.y);
if (line_same(s0, s1)) {
return (q1.x <= p2.x && p1.x <= q2.x && q1.y <= p2.y && p1.y <= q2.y);
}
else return 0;
}
return cross_2d(p2 - p1, q1 - p1) * cross_2d(p2 - p1, q2 - p1) < eps
&& cross_2d(q2 - q1, p1 - q1) * cross_2d(q2 - q1, p2 - q1) < eps;
}
vector<point> circles_intersection(circle A, circle B) {
vector<point> ret;
point X = A.p;
B.p = B.p - A.p;
A.p = {0, 0};
if (points_distance(A.p, B.p, 2) - pow(A.r + B.r, 2) > eps) return ret;
double K = (B.p.x*B.p.x + B.p.y*B.p.y + A.r*A.r - B.r*B.r) / 2;
double SQ = (B.p.x*B.p.x + B.p.y*B.p.y)*A.r*A.r - K * K;
double DS = points_distance(A.p, B.p, 2);
//cerr << K _ SQ _ DS << endl;
if (abs(SQ) < eps) {
ret.push_back({K*B.p.x / DS + X.x, K*B.p.y / DS + X.y});
return ret;
}
ret.push_back({(K*B.p.x + B.p.y*sqrt(SQ)) / DS + X.x,
(K*B.p.y - B.p.x*sqrt(SQ)) / DS + X.y});
ret.push_back({(K*B.p.x - B.p.y*sqrt(SQ)) / DS + X.x,
(K*B.p.y + B.p.x*sqrt(SQ)) / DS + X.y});
return ret;
}
int main() {
int n;
cin >> n;
point p[n];
for (int i = 0; i < n; i++) cin >> p[i].x >> p[i].y;
for (int i = 0; i < n; i++) {
if (cross_2d(p[(i + 1) % n] - p[i], p[(i + 2) % n] - p[(i + 1) % n]) < 0) {
cout << 0 << endl;
return 0;
}
}
cout << 1 << endl;
}
|
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;
#define PB push_back
const double eps=1e-9;
double ABS(double n){return n>=0?n:-n;}
double min(double a,double b){return a>b?b:a;}
double max(double a,double b){return a>b?a:b;}
bool same(double a,double b){return ABS(a-b)<eps;}
struct point{
double x;
double y;
point(){}
point(double a,double b){x=a;y=b;}
point operator +(const point &a){return point(x+a.x,y+a.y);}
point operator -(const point &a){return point(x-a.x,y-a.y);}
point operator *(const double &a){return point(x*a,y*a);}
point operator /(const double &a){return point(x/a,y/a);}
void operator =(const point &a){x=a.x;y=a.y;}
void operator +=(const point &a){x+=a.x;y+=a.y;}
void operator -=(const point &a){x-=a.x;y-=a.y;}
void operator *=(const double &a){x*=a;y*=a;}
void operator /=(const double &a){x/=a;y/=a;}
bool operator <(const point &a){return x<a.x||(same(x,a.x)&&y<a.y);}
bool operator ==(const point &a){return same(x,a.x)&&same(y,a.y);}
double length(){return sqrt(x*x+y*y);}
void in(){scanf("%lf%lf",&x,&y);}
void out(){printf("%.20lf %.20lf\n",x,y);}
};
struct segment{
point a;
point b;
segment(){}
segment(point x,point y){a=x;b=y;}
};
struct line{
double A;
double B;
double C;
line(){}
line(double a,double b,double c){A=a;B=b;C=c;}
line(point a,point b){
A=a.y-b.y;
B=b.x-a.x;
C=A*a.x+B*a.y;
}
line(double a,double b,point p){
A=a;
B=b;
C=A*p.x+B*p.y;
}
line(segment s){
A=s.a.y-s.b.y;
B=s.b.x-s.a.x;
C=A*s.a.x+B*s.a.y;
}
};
double distance(point a,point b){return (a-b).length();}
double dot(point a,point b){return a.x*b.x+a.y*b.y;}
double cross(point a,point b){return a.x*b.y-b.x*a.y;}
//angle only returns positive value
double angle(point a,point b){return acos(dot(a,b)/(a.length()*b.length()));}
double area(point a,point b,point c){return ABS(cross(b-a,c-a));}
double distance(point a,line b){return (b.A*a.x+b.B*a.y-b.C)/sqrt(b.A*b.A+b.B*b.B);}
bool between(point a,point b,point c){
if((a.x-b.x<=eps&&b.x-c.x<=eps)||(eps>=b.x-a.x&&eps>=c.x-b.x))
if((a.y-b.y<=eps&&b.y-c.y<=eps)||(eps>=b.y-a.y&&eps>=c.y-b.y))
return true;
return false;
}
point intersect(line a,line b){
double x,y,det;
x=(a.C*b.B-b.C*a.B);
y=(a.A*b.C-b.A*a.C);
det=(a.A*b.B-b.A*a.B);
return point(x,y)/det;
}
bool parallel(line a,line b){return same(a.A*b.B,b.A*a.B);}
double distance(line a,line b){}
double distance(point p,segment s){
line l(s);
point a(intersect(line(l.B,-l.A,l.B*p.x-l.A*p.y),l));
if(between(s.a,a,s.b))return distance(p,a);
else return min(distance(p,s.a),distance(p,s.b));
}
double distance(segment a,segment b){
point l(a.a),lm,rm,r(a.b);
for(int i=0;i<100;i++){
lm=(l*2+r)/3;
rm=(l+r*2)/3;
if(distance(lm,b)>distance(rm,b))l=lm;
else r=rm;
}
return distance(l,b);
}
bool same(line a,line b){return distance(a,b)<=eps;}
bool vertical(line a,line b){return ABS(a.A*b.A+a.B*b.B)<=eps;}
bool hasintersect(segment a,segment b){
line l(a),r(b);
if(parallel(l,r)){
if(same(l,r))return between(a.a,b.a,a.b)||between(a.a,b.b,a.b)||between(b.a,a.a,b.b)||between(b.a,a.b,b.b);
else return false;
}
else{
point p(intersect(l,r));
return between(a.a,p,a.b)&&between(b.a,p,b.b);
}
}
point intersect(segment a,segment b){return intersect(line(a),line(b));}
point projection(point a,line b){return intersect(line(b.B,-b.A,a),b);}
point reflection(point a,line b){return projection(a,b)*2-a;}
bool online(point p,line l){return ABS(l.A*p.x+l.B*p.y-l.C)<eps;}
bool onseg(point p,segment s){return online(p,line(s))&&between(s.a,p,s.b);}
double area(vector<point> &v){
double ans=0;
int n=(int)v.size();
for(int i=1;i<n;i++)ans+=cross(v[i],v[i-1]);
ans+=cross(v[0],v[n-1]);
return ABS(ans)/2;
}
bool isconvex(vector<point> &v){
bool f=true;
int n=(int)v.size();
vector<point> nv(v);
nv.PB(v[0]);
nv.PB(v[1]);
for(int i=0;i<n;i++)if(cross(v[i+1]-v[i],v[i+2]-v[i])<-eps){
f=false;
break;
}
if(f)return true;
f=true;
reverse(nv.begin(),nv.end());
for(int i=0;i<n;i++)if(cross(v[i+1]-v[i],v[i+2]-v[i])<-eps)return false;
return true;
}
int main(){
int n;
vector<point> v;
point temp;
scanf("%d",&n);
for(int i=0;i<n;i++){
temp.in();
v.PB(temp);
}
if(isconvex(v))printf("1\n");
else printf("0\n");
}
|
/* Header {{{ */
#include <bits/stdc++.h>
using namespace std;
typedef long long readtype;
typedef long long var;
typedef long double let;
readtype read() {
readtype a = 0, c = getchar(), s = 0;
while (!isdigit(c)) s |= c == '-', c = getchar();
while (isdigit(c)) a = a * 10 + c - 48, c = getchar();
return s ? -a : a;
}
#ifdef LOCAL_LOGGER
#define logger(...) fprintf(stderr, __VA_ARGS__)
#define abortif(v, ...) if (v) {logger("Error in Line %d, Function '%s()'.\nInfo: ", __LINE__, __FUNCTION__); logger(__VA_ARGS__); exit(0);}
#else
#define logger(...);
#define abortif(v, ...);
#endif
/* }}} */
const int N = 101;
struct Point {
let x, y;
friend let Times(Point a, Point b) {
return a.x * b.y - a.y * b.x;
}
friend Point operator - (Point a, Point b) {
return (Point) {a.x - b.x, a.y - b.y};
}
};
bool CheckConvex(int n, Point *point) {
if (Times(point[2] - point[1], point[3] - point[2]) < 0)
reverse(point + 1, point + n + 1);
for (int i = 1; i < n - 1; ++i) {
if (Times(point[i + 1] - point[i], point[i + 2] - point[i + 1]) < 0)
return false;
}
if (Times(point[n] - point[n - 1], point[1] - point[n]) < 0) return false;
return true;
}
Point ReadPoint() {
Point p;
scanf("%Lf%Lf", &p.x, &p.y);
return p;
}
Point point[N];
int main() {
// #ifndef ONLINE_JUDGE
// freopen("CGL_3_B.in", "r", stdin);
// freopen("CGL_3_B.out", "w", stdout);
// #endif
// #ifdef LOCAL_LOGGER
// freopen("CGL_3_B.log", "w", stderr);
// #endif
int n = read();
for (int i = 1; i <= n; ++i) point[i] = ReadPoint();
puts(CheckConvex(n, point) ? "1" : "0");
return 0;
}
/* ==== Makefile ==== {{{
CompileAndRun:
make Compile
make Run
Compile:
g++ -o CGL_3_B CGL_3_B.cpp -g -Wall -DLOCAL_LOGGER
CompileUF:
g++ -o CGL_3_B CGL_3_B.cpp -g -Wall -DLOCAL_LOGGER -fsanitize=undefined
Run:
./CGL_3_B < CGL_3_B.in > CGL_3_B.out
==================
}}} */
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long; using ld = long double; using ull = unsigned long long; using uint = unsigned int;
using vi = vector<int>; using vb = vector<bool>; using vd = vector<double>; using vl = vector<ll>;
using vvi = vector<vi>; using vvb = vector<vb>; using vvd = vector<vd>; using vvl = vector<vl>;
#define REP(i,n) for(ll i=0; i<(n); ++i)
#define FOR(i,b,n) for(ll i=(b); i<(n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define TEN(x) ((ll)1e##x)
template<typename T> inline string join(const vector<T>& vec, string sep = " ") { stringstream ss; REP(i, vec.size()) ss << vec[i] << ( i+1 == vec.size() ? "" : sep ); return ss.str(); }
///////////////
#define EPS (1e-10)
#define NEXT(x, i) (x[(i + 1) % x.size()])
template<class T> using CR = const T &;
using P = complex<ld>;
using G = vector<P>;
int sgn(ld a, ld b = 0) {
if (a > b + EPS) return 1;
if (a < b - EPS) return -1;
return 0;
}
ld dot(P a, P b) { return real(conj(a)*b); }
ld cross(P a, P b) { return imag(conj(a)*b); }
int ccw(P a, P b, P c) {
b -= a;
c -= a;
if (sgn(cross(b, c))) return sgn(cross(b, c)); // clockwise or counter clockwise
if (sgn(dot(b, c)) == -1) return 2; // c--a--b
if (sgn(norm(b), norm(c)) == -1) return -2; // a--b--c
return 0; // a--c--b
}
#define PREV(x, i) (x[(i+x.size()-1) % x.size()])
#define NEXT(x, i) (x[(i + 1) % x.size()])
bool is_convex(CR<G> g) {
REP(i, g.size()) if (ccw(PREV(g, i), g[i], NEXT(g, i)) == -1) return false;
return true;
}
//////////////
int main() {
#ifdef INPUT_FROM_FILE
ifstream cin("sample.in");
ofstream cout("sample.out");
#endif
cin.tie(0);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(30);
ll n; cin >> n;
G g(n);
REP(i,n){
ld x, y;
cin >> x >> y;
g[i] = { x, y };
}
cout << is_convex(g) << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long unsigned int ll;
// 参考サイト
// http://www.prefield.com/algorithm/index.html
const double EPS = 1e-8;
const double INF = 1e12;
typedef complex<double> Point; //複素数で平面定義
typedef Point P;
// system {{{
// オペレーター< を定義、後々楽
namespace std{
bool operator < ( const P& a, const P& b) {
return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}
}
// 外積
double cross( const P& a, const P& b ){
return imag(conj(a)*b);
}
// 内積
double dot( const P& a, const P& b ){
return real(conj(a)*b);
}
// }}}
// Line L:vector<P> PolyGon G:vector<P> Circle C(P,int rad) {{{
// 直線 Line 線分 Segment {{{
struct L : public vector<P> {
L(const P &a, const P &b ){
push_back(a); push_back(b);
}
};
// }}}
// 単純多角形 Polygon {{{
struct G : public vector<P> {
P curr( int i ){
return *(begin()+i);
}
P next( int i ){
return *(begin()+(i+1)%size());
}
P prev( int i ){
i += size();
return *(begin()+(i-1)%size());
}
double area2(){
double A = 0.0;
for( int i = 0; i < size(); i++ ){
A += cross( curr(i),next(i) );
}
return A;
}
};
using Polygon = G;
//}}}
// 円 cirlce {{{
struct C {
P p; double r;
C(const P &p, double r ) : p(p), r(r) {}
};
// }}}
// }}}
// counter clockwise {{{
//
int ccw( P a, P b, P c ){
b -= a; c -= a;
if( cross(b,c) > 0 ) return +1; //counter clockwise
if( cross(b,c) < 0 ) return -1; //clockwise
if( dot(b,c) < 0 ) return +2; //online_back
if( norm(b) < norm(c)) return -2; //online_front
return 0; // on_segment
} //}}}
// 交点判定 交点座標 LSPtoLSP {{{
bool intersectLL( const L &l, const L &m ){
return abs( cross(l[1]-l[0], m[1]-m[0]) ) > EPS || // cross(l,m) != 0 <-> not paralell
abs( cross(l[1]-l[0], m[0]-l[0]) ) < EPS; // cross(l,(m-l)) == 0 <-> same line
}
bool intersectLS( const L &l, const L &s ){
return cross( l[1]-l[0], s[0]-l[0] ) *
cross( l[1]-l[0], s[1]-l[0] ) < EPS;
}
bool intersectLP( const L &l, const P &p ){
return abs( cross(l[0]-p, l[1]-p) );
}
bool intersectSS( const L &s, const L &t ){
return ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= 0 &&
ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= 0;
}
bool intersectSP( const L &s, const P &p ){
return abs( s[0]-p )+abs( s[1]-p )-abs(s[1]-s[0]) < EPS;
}
P crossPoint( const L &l, const L &m ){
double A = cross( l[1] - l[0], m[1] - m[0] );
double B = cross( l[1] - l[0], l[1] - m[0] );
// A は に直線の成す平行四辺形
// B は そのA のうちの片側
// 書いたら分かる
// 参考サイト
// http://www.fumiononaka.com/Business/html5/FN1312003.html
if( abs(A) < EPS && abs(B) < EPS ) return m[0];
return m[0] + B / A * ( m[1] - m[0] );
}
// }}}
// 射影 反射 距離 LSPtoLSP {{{
P projection( const L &l, const P &p ){
double t = dot( p-l[0], l[0]-l[1] ) / norm( l[0]-l[1] );
return l[0] + t*(l[0]-l[1]);
}
P reflection( const L &l, const P &p ){
return p + 2.0*( projection(l,p) - p );
}
double distanceSP( const L &s, const P &p ){
const P r = projection(s,p);
if( intersectSP(s,r) ) return abs(r-p);
else return min( abs(s[0] - p), abs(s[1] - p) );
}
double distanceSS( const L &s, const L &t ){
if( intersectSS(s,t) ) return 0.0;
return min( min( distanceSP(s,t[0]), distanceSP(s,t[1]) ),
min( distanceSP(t,s[0]), distanceSP(t,s[1]) ) );
}
// }}}
// 多角形面積 G {{{
double area2( const Polygon &P ){
double A = 0.0;
for( int i = 0; i < P.size(); i++ ){
A += cross( P[i], P[(i+1)%P.size()] );
}
return A;
}
// }}}
// imagePointDescription(点表示) {{{
void imagePointDescription( const vector<P> &p, double scale = 1 ){
int here[51][51] = {};
int i = 0;
for( P t : p ){
i++;
int y = round(imag(t)/scale);
int x = round(real(t)/scale);
if( abs(y) > 25 ) continue;
if( abs(x) > 25 ) continue;
here[y+25][x+25] = i;
}
for( i = 50; i >= 0; i-- ){
for( int j = 0; j <= 50; j++ ){
if( here[i][j] ) printf ("%2d", here[i][j] );
else if( i == 25 && j == 25 ) printf ("-+");
else if( i == 25 ) printf ("--");
else if( j == 25 ) printf (" |");
else if( j % 5 == 0 && i % 5 == 0)printf (" .");
else printf (" ");
}
printf ("\n");
}
}
// }}}
int main() {
double a,b,c,d;
G pol;
int q;
cin >> q;
int n = q;
while(q--){
cin >> a >> b;
P p(a,b);
pol.push_back(p);
// printf ("%1.10lf %1.10lf\n", real(x), imag(x) );
}
// imagePointDescription(pol,1);
int tmp = 0;
int ans = 1;
for( int i = 2; i < n; i++ ){
int ccwt = ccw( pol.prev(i), pol.curr(i), pol.next(i) );
if( ccwt == -2 ) continue;
if( tmp ) if( ccwt != tmp ) ans = 0;
tmp = ccwt;
}
printf ("%d\n", ans );
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef double db;
const db eps = 1e-15, pi = acos(-1);
int sign(db x) {return x < -eps ? -1 : x > eps;}
int cmp(db x, db y) {return sign(x - y);}
int intersect(db l1, db r1, db l2, db r2) {
if (l1 > r1) swap(l1, r1); if (l2 > r2) swap(l2, r2); return cmp(r1, l2) != -1 && cmp(r2, l1) != -1;
}
int inmid(db k1, db k2, db k3) {return sign(k1 - k3) * sign(k2 - k3) <= 0;}//k3 in [k1,k2]?1:0
struct Point {
db x, y;
Point operator + (const Point & a)const {return Point{a.x + x, a.y + y};}
Point operator - (const Point & a)const {return Point{x - a.x, y - a.y};}
Point operator * (db a) const {return Point{x * a, y * a};}
Point operator / (db a) const {return Point{x / a, y / a};}
bool operator < (const Point p) const {int a = cmp(x, p.x); if (a) return a == -1; return cmp(y, p.y) == -1;}
bool operator == (const Point & a) const {return cmp(x, a.x) == 0 && cmp(y, a.y) == 0;}
db abs() {return sqrt(x * x + y * y);}
db dis(Point p) {return ((*this) - p).abs();}
int getP() const {return sign(y) == 1 || (sign(y) == 0 && sign(x) == -1);}
void input() {scanf("%lf%lf", &x, &y);}
};
db cross(Point p1, Point p2) {return p1.x * p2.y - p1.y * p2.x;}
db cross(Point p0, Point p1, Point p2) {return cross(p1 - p0, p2 - p0);}
db dot(Point p1, Point p2) {return p1.x * p2.x + p1.y * p2.y;}
int inmid(Point k1, Point k2, Point k3) {return inmid(k1.x, k2.x, k3.x) && inmid(k1.y, k2.y, k3.y);}
bool compareangle(Point p1, Point p2) {//Polar Angle Sort
return p1.getP() < p2.getP() || (p1.getP() == p2.getP() && sign(cross(p1, p2)) > 0);
}
int clockwise(Point p1, Point p2, Point p3) { // k1 k2 k3 anticlockwise:1 clockwise:-1 others:0
return sign(cross(p1, p2, p3));
}
struct Line {
Point s, e;
void input() {scanf("%lf%lf%lf%lf", &s.x, &s.y, &e.x, &e.y);}
Point vec() {return e - s;}
db length() {return sqrt(dot(s - e, s - e));}
db length2() {return dot(s - e, s - e);}
};
int onS(Line l, Point p) {// On Seg?
return inmid(l.s, l.e, p) && sign(cross(l.s - p, l.e - l.s)) == 0;
}
bool checkLL(Line l1, Line l2) {
return cmp(cross(l1.s, l2.s, l2.e), cross(l1.e, l2.s, l2.e)) != 0;
}
bool checkLS(Line l1, Line l2) {//Intersection of Line l1 and Seg l2?
return sign(cross(l2.s, l1.s, l1.e)) * sign(cross(l2.e, l1.s, l1.e)) <= 0;
}
int checkSS(Line l1, Line l2) {//Intersection of Two Seg?1:0
return intersect(l1.s.x, l1.e.x, l2.s.x, l2.e.x) && intersect(l1.s.y, l1.e.y, l2.s.y, l2.e.y) && checkLS(l1, l2) && checkLS(l2, l1);
}
Point project(Line l, Point p) {
return l.s + l.vec() * dot(p - l.s, l.vec()) / l.length2();
}
Point reflect(Line l, Point p) {//Mirror Point
return project(l, p) * 2 - p;
}
Point getLL(Line l1, Line l2) {//Intersection Point of Line l1,l2
db w1 = cross(l2.s, l1.s, l2.e), w2 = cross(l2.s, l2.e, l1.e); return (l1.s * w2 + l1.e * w1) / (w1 + w2);
}
db disSP(Line l, Point p) {
Point p2 = project(l, p);
if (inmid(l.s,l.e,p2)) return p.dis(p2); else return min(p.dis(l.s), p.dis(l.e));
}
db disSS(Line l1, Line l2) {
if (checkSS(l1, l2)) return 0;
return min(min(disSP(l1, l2.s), disSP(l1, l2.e)), min(disSP(l2, l1.s), disSP(l2, l1.e)));
}
db area(vector<Point> A) {//Anticlockwise
db ans = 0;
for (int i = 0; i < A.size(); i++) ans += cross(A[i], A[(i + 1) % A.size()]);
return ans / 2;
}
int contain(vector<Point>A, Point p) {//2:in 1:on 0:out
int ans = 0; A.push_back(A[0]);
for (int i = 1; i < A.size(); i++) {
Line l = {A[i - 1], A[i]};
if (onS(l, p)) return 1; if (cmp(l.s.y, l.e.y) > 0) swap(l.s, l.e);
if (cmp(l.s.y, p.y) >= 0 || cmp(l.e.y, p.y) < 0) continue;
if (sign(cross(l.e, l.s, p)) < 0)ans ^= 1;
}
return ans << 1;
}
bool checkconvex(vector<Point>A){//anticlock
int n=A.size(); A.push_back(A[0]); A.push_back(A[1]);
for (int i=0;i<n;i++) if (sign(cross(A[i],A[i+1],A[i+2]))==-1) return 0;
return 1;
}
int main() {
int n;cin>>n;vector<Point> A(n);
for(int i=0;i<A.size();++i)A[i].input();
cout<<checkconvex(A)<<endl;
}
|
#include<bits/stdc++.h>
#define For(i,x,y) for (register int i=(x);i<=(y);i++)
#define FOR(i,x,y) for (register int i=(x);i<(y);i++)
#define Dow(i,x,y) for (register int i=(x);i>=(y);i--)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define fil(a,b) memset((a),(b),sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pa;
typedef double db;
inline ll read(){
ll x=0,f=1;char c=getchar();
while ((c<'0'||c>'9')&&(c!='-')) c=getchar();
if (c=='-') f=-1,c=getchar();
while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
return x*f;
}
namespace Geometry{
const db eps = 1e-9;
struct point{
db x,y;
inline point operator + (const point &p){return (point){x+p.x,y+p.y};}
inline point operator - (const point &p){return (point){x-p.x,y-p.y};}
inline db operator * (const point &p){return x*p.x+y*p.y;}
inline point operator / (const db &v){return (point){x/v,y/v};}
inline point operator * (const db &v){return (point){x*v,y*v};}
};
typedef point vec;
inline db dot(vec a,vec b){return a.x*b.x+a.y*b.y;}
inline db sqr(db x){return x*x;}
inline db dis(point a,point b){return sqrt(sqr(a.x-b.x)+sqr(a.y-b.y));}
inline db dis2(point a,point b){return sqr(a.x-b.x)+sqr(a.y-b.y);}
inline db cross(vec a,vec b){return a.x*b.y-a.y*b.x;}
struct line{
point a,b;
};
inline point projection(point A,line b){
point B=b.a,C=b.b;
vec BA=A-B,BC=C-B,BD=BC*(BA*BC/dis2(B,C));
return B+BD;
}
inline point reflection(point A,line b){
point D=projection(A,b);
return (point){D.x*2-A.x,D.y*2-A.y};
}
inline int Counter_Clockwise(point A,line b){
point B=b.a,C=b.b;
db f1=cross(C-B,A-B);
if (fabs(f1)>eps) return (f1<0?2:1);
db f2=(C-B)*(A-B);
if (f2<0) return 3;
return dis(B,C)<dis(A,B)?4:5;
}
inline int checkline(line a,line b){
vec A=a.b-a.a,B=b.b-b.a;
db f1=A*B;
if (fabs(f1)<=eps) return 1;
return dis(a.a,a.b)*dis(b.a,b.b)-fabs(f1)<=eps?2:0;
}
inline int checkseg(line a,line b){
point A=a.a,B=a.b,C=b.a,D=b.b;
if (max(A.x,B.x)<min(C.x,D.x)) return 0;
if (min(A.x,B.x)>max(C.x,D.x)) return 0;
if (max(A.y,B.y)<min(C.y,D.y)) return 0;
if (min(A.y,B.y)>max(C.y,D.y)) return 0;
db s=cross(B-A,C-A),h=cross(B-A,D-A);
if (s*h>=eps) return 0;
db i=cross(D-C,A-C),t=cross(D-C,B-C);
return i*t<=eps;
}
inline point crosspoint(line a,line b){
point A=a.a,B=a.b,C=b.a,D=b.b;
db s1=fabs(cross(B-A,C-A)),s2=fabs(cross(B-A,D-A));
vec CO=(D-C)*(s1/(s1+s2));
return C+CO;
}
inline db dis(point A,line b){
point B=b.a,C=b.b,D=projection(A,b);
int tmp=Counter_Clockwise(D,b);
if (tmp==3) return dis(B,A);
if (tmp==4) return dis(C,A);
if (tmp==5) return dis(D,A);
}
inline db dis(line a,line b){
if (checkseg(a,b)) return 0;
db s=dis(a.a,b),h=dis(a.b,b),i=dis(b.a,a),t=dis(b.b,a);
return min({s,h,i,t});
}
typedef vector<point> polygon;
inline db S(polygon v){
db s=cross(v.back(),v[0]);
For(i,0,siz(v)-2) s+=cross(v[i],v[i+1]);
return s/2;
}
inline bool check(polygon v){
int n=siz(v),f=(cross(v[n-1]-v[n-2],v[0]-v[n-1])>=-eps);
For(i,1,n-2) if ((cross(v[i]-v[i-1],v[i+1]-v[i])>=-eps)!=f) return 0;
return 1;
}
};
using namespace Geometry;
int main(){
// freopen("data.in","r",stdin);
int n=read();polygon v(n);
FOR(i,0,n) scanf("%lf%lf",&v[i].x,&v[i].y);
printf("%d\n",check(v));
}
|
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <cmath>
#include <cstdio>
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define F first
#define S second
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if(a < b){ a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if(a > b){ a = b; return 1; } return 0; }
using ll = long long;
using pi = pair<int,int>;
const double EPS = 1e-10;
struct Vec2 {
double x;
double y;
Vec2() : x(0.0),y(0.0){}
Vec2(double _x,double _y) :x(_x),y(_y){}
Vec2 operator+(const Vec2& other) const { return { x + other.x , y + other.y }; }
Vec2 operator-(const Vec2& other) const { return { x - other.x , y - other.y }; }
bool operator==(const Vec2& other) const { return (abs(x - other.x) < EPS && abs(y - other.y) < EPS ); }
double dot(const Vec2& other) const { return x * other.x + y * other.y; }
double cross(const Vec2& other) const { return x * other.y - y * other.x; }
double length() const { return sqrt(x * x + y * y); }
};
// A とB のなす角θ cosθ, sinθ を返す
double Cos(const Vec2 A, const Vec2 B) { return A.dot(B) / (A.length()*B.length()); }
double Sin(const Vec2 A, const Vec2 B) { return A.cross(B) / (A.length()*B.length()); }
int main()
{
int n; cin >> n;
vector<Vec2> v(n);
rep(i,n) {
cin >> v[i].x >> v[i].y;
}
bool f = true;
rep(i,n) {
if(Sin(v[(i+1)%n] - v[i], v[(i+2)%n] - v[i]) < 0) f = false;
}
if(f) cout << 1 << "\n";
else cout << 0 << "\n";
return 0;
}
|
#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
using namespace std;
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define FORR(i,a,b) for (int i=(a);i>=(b);i--)
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9+7;
const double EPS = 1e-9;
int main() {
int N;
cin >> N;
vector<int> x(N+2), y(N+2);
FOR(i, 0, N) {
cin >> x[i] >> y[i];
}
x[N] = x[0]; y[N] = y[0];
x[N+1] = x[1]; y[N+1] = y[1];
auto cross = [](double a, double b, double c, double d) -> double {
return a * d - b * c;
};
int result = 1;
FOR(i, 0, N) {
double p = cross(x[i+1]-x[i], y[i+1]-y[i],
x[i+2]-x[i+1], y[i+2]-y[i+1]);
if (p < 0) {
result = 0;
break;
}
}
cout << result << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
constexpr double EPS = 1e-10;
constexpr int COUNTER_CLOCKWISE = 1;
constexpr int CLOCKWISE = -1;
constexpr int ONLINE_BACK = 2;
constexpr int ONLINE_FRONT = -2;
constexpr int ON_SEGMENT = 0;
bool equals(double a, double b) { return (abs(a - b) < EPS); }
struct V2 {
double x, y;
V2(double x = 0, double y = 0) : x(x), y(y) {}
V2 operator + (V2 p) const { return (V2(x + p.x, y + p.y)); }
V2 operator - (V2 p) const { return (V2(x - p.x, y - p.y)); }
V2 operator * (double r) const { return (V2(x * r, y * r)); }
V2 operator / (double r) const { return (V2(x / r, y / r)); }
double norm() const { return (sqrt(sqrNorm())); }
double sqrNorm() const { return (x*x + y*y); }
bool operator < (const V2 &p) const {
return (x != p.x ? x < p.x : y < p.y);
}
bool operator == (const V2 &p) const {
return (equals(x, p.x) && equals(y, p.y));
}
V2 rotate90() const { return (V2(y, -x)); }
V2 normalized() const { return (*this / norm()); }
double dot(const V2 &p) const { return (x*p.x + y*p.y); }
double cross(const V2 &p) const { return (x*p.y - y*p.x); }
double arg() const { return (atan2(y, x)); }
};
V2 polar(double r, double a) { return (V2(cos(a) * r, sin(a) * r)); }
int ccw(V2 p0, V2 p1, V2 p2) {
V2 a = p1-p0, b = p2-p0;
if (a.cross(b) > EPS) return (COUNTER_CLOCKWISE);
if (a.cross(b) < -EPS) return (CLOCKWISE);
if (a.dot(b) < -EPS) return (ONLINE_BACK);
if (a.sqrNorm() < b.sqrNorm()) return (ONLINE_FRONT);
return (ON_SEGMENT);
}
using Polygon = vector<V2>;
//verified with https://onlinejudge.u-aizu.ac.jp/courses/library/4/CGL/all/CGL_3_C
int containP(const Polygon &g, V2 p) {
int size = g.size();
bool x = false;
for (int i = 0; i < size; i++) {
V2 a = g[i]-p, b = g[(i+1)%size]-p;
if (abs(a.cross(b)) < EPS && a.dot(b) < EPS) return (1);
if (a.y > b.y) swap(a, b);
x ^= (a.y < EPS && EPS < b.y && a.cross(b) > EPS);
}
return (x ? 2 : 0);
}
struct Segment {
V2 p1, p2;
Segment() {}
Segment(V2 p1, V2 p2) : p1(p1), p2(p2) {}
};
using Line = Segment;
struct Circle {
V2 o;
double r;
Circle() {}
Circle(V2 o, double r) : o(o), r(r) {}
int intersects(const Circle &c) {
double d = (o-c.o).norm();
if (d + c.r < r) return (3); //contain on A
if (d + r < c.r) return (-3); //contain on B
if (equals(d + c.r, r)) return (1); //inscribed on A
if (equals(d + r, c.r)) return (-1); //inscribed on B
if (r + c.r < d) return (0); //circumscribed
if (equals(r + c.r, d)) return (4); //far
return (2); //intersected
}
vector<V2> crossPoints(const Circle &c) {
int inter = intersects(c);
if (abs(inter) == 3 || inter == 0) return (vector<V2>());
double d = (c.o-o).norm();
double t = (c.o-o).arg();
if (abs(inter) == 1 || inter == 4) return ((vector<V2>){o + (c.o-o).normalized() * r});
double a = acos((r*r + d*d - c.r*c.r) / (2*r*d));
return ((vector<V2>){o + polar(r, t+a), o + polar(r, t-a)});
}
};
bool isConvex(const Polygon &data) {
int size = data.size();
for (int i = 0; i < size; i++) {
int j = i+1, k = i+2;
if (j >= size) j -= size;
if (k >= size) k -= size;
if (ccw(data[i], data[j], data[k]) == CLOCKWISE) {
return (false);
}
}
return (true);
}
Polygon ConvexHull(Polygon data) {
int size = data.size();
Polygon u, l;
sort(begin(data), end(data));
if (size < 3) return (data);
u.push_back(data[0]); u.push_back(data[1]);
l.push_back(data[size-1]); l.push_back(data[size-2]);
for (int i = 2; i < size; i++) {
for (int sz = u.size(); sz > 1 && ccw(u[sz-2], u[sz-1], data[i]) != CLOCKWISE; sz--) {
u.pop_back();
}
u.push_back(data[i]);
}
for (int i = size-3; i >= 0; i--) {
for (int sz = l.size(); sz > 1 && ccw(l[sz-2], l[sz-1], data[i]) != CLOCKWISE; sz--) {
l.pop_back();
}
l.push_back(data[i]);
}
reverse(begin(l), end(l));
for (int i = u.size()-2; i >= 1; i--) {
l.push_back(u[i]);
}
return (l);
}
int main() {
int N; cin >> N;
Polygon points(N);
for (auto &p : points) cin >> p.x >> p.y;
cout << isConvex(points) << endl;
}
|
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
// ------ Classes ------ //
class Point {
public:
long double px, py;
Point() : px(0), py(0) {};
Point(long double px_, long double py_) : px(px_), py(py_) {};
friend bool operator==(const Point& p1, const Point& p2) { return p1.px == p2.px && p1.py == p2.py; }
friend bool operator!=(const Point& p1, const Point& p2) { return p1.px != p2.px || p1.py != p2.py; }
friend bool operator<(const Point& p1, const Point& p2) { return p1.px < p2.px ? true : (p1.px == p2.px && p1.py < p2.py); }
friend bool operator>(const Point& p1, const Point& p2) { return p1.px > p2.px ? true : (p1.px == p2.px && p1.py > p2.py); }
friend bool operator<=(const Point& p1, const Point& p2) { return !(p1 > p2); }
friend bool operator>=(const Point& p1, const Point& p2) { return !(p1 < p2); }
friend Point operator+(const Point& p1, const Point& p2) { return Point(p1.px + p2.px, p1.py + p2.py); }
friend Point operator-(const Point& p1, const Point& p2) { return Point(p1.px - p2.px, p1.py - p2.py); }
friend Point operator*(const Point& p1, long double d) { return Point(p1.px * d, p1.py + d); }
friend Point operator*(long double d, const Point& p1) { return p1 * d; }
friend Point operator/(const Point& p1, long double d) { return Point(p1.px / d, p1.py / d); }
Point& operator+=(const Point& p1) { px += p1.px; py += p1.py; return *this; }
Point& operator-=(const Point& p1) { px -= p1.px; py -= p1.py; return *this; }
Point& operator*=(long double d) { px *= d; py *= d; return *this; }
Point& operator/=(long double d) { px /= d; py /= d; return *this; }
};
// ------ Functions ------ //
long double norm(const Point& a) { return a.px * a.px + a.py * a.py; }
long double abs(const Point& a) { return sqrtl(norm(a)); }
long double dot(const Point& a, const Point& b) { return a.px * b.px + a.py * b.py; }
long double crs(const Point& a, const Point& b) { return a.px * b.py - a.py * b.px; }
int ccw(Point p0, Point p1, Point p2) {
Point a = p1 - p0, b = p2 - p0;
if (crs(a, b) > 1e-10) return 1;
if (crs(a, b) < -1e-10) return -1;
if (dot(a, b) < -1e-10) return 2;
if (norm(a) < norm(b)) return -2;
return 0;
}
bool is_convex(vector<Point> v) {
if (v.size() < 3) return false;
int s = ccw(v[0], v[v.size() - 1], v[1]);
for (int i = 0; i < v.size(); i++) {
int r = ccw(v[i], v[(i + v.size() - 1) % v.size()], v[(i + 1) % v.size()]);/*
if (s == 1 && r == -1) return false;
if (s == -1 && r == 1) return false;*/
if (r == 1) return false;
}
return true;
}
// ------ Main ------ //
int n; vector<Point> v;
int main() {
cin >> n; v.resize(n);
for (int i = 0; i < n; i++) cin >> v[i].px >> v[i].py;
cout << (is_convex(v) ? 1 : 0) << endl;
} |
#include <algorithm>
#include <vector>
#include <cfloat>
#include <string>
#include <cmath>
#include <set>
#include <cstdlib>
#include <map>
#include <ctime>
#include <iomanip>
#include <functional>
#include <deque>
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
#include <stack>
#include <climits>
#include <sys/time.h>
#include <cctype>
using namespace std;
typedef long long ll;
#define EPS (1e-10)
#define equals(a, b) (fabs((a)-(b)) < EPS)
class Point {
public:
double x, y;
Point(double x = 0, double y = 0): x(x), y(y) {}
Point operator + (Point p) { return Point(x+p.x, y+p.y); }
Point operator - (Point p) { return Point(x-p.x, y-p.y); }
Point operator * (double a) { return Point(a*x, a*y); }
Point operator / (double a) { return Point(x/a, y/a); }
double norm() { return x*x+y*y; }
double abs() { return sqrt(norm()); }
bool operator < (const Point &p) const {
return x != p.x ? x < p.x : y < p.y;
}
bool operator == (const Point &p) const {
return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;
}
};
typedef Point Vector;
typedef vector <Vector> Polygon;
class Segment {
public:
Point p1, p2;
Segment(Point p1 = Point(), Point p2 = Point()): p1(p1), p2(p2) {}
};
typedef Segment Line;
double norm(Vector a) {
return a.x*a.x+a.y*a.y;
}
double abs(Vector a) {
return sqrt(norm(a));
}
// ?????????|a||b|cos
double cross(Vector a, Vector b) {
return a.x*b.y-a.y*b.x;
}
// ?????????|a||b|sin
double dot(Vector a, Vector b) {
return a.x*b.x+a.y*b.y;
}
// 1:???????¨??????? -1:????¨??????? 2:p2-p0-p1 -2:p0-p1-p2 0:p2???p0-p1???
int ccw(Point p0, Point p1, Point p2) {
Vector a = p1-p0;
Vector b = p2-p0;
if (cross(a, b) > EPS) return 1;
if (cross(a, b) < -EPS) return -1;
if (dot(a, b) < -EPS) return 2;
if (a.norm() < b.norm()) return -2;
return 0;
}
// ??????(??¢???????????\????????¢?????´??????????????????):????????????????????????????????????????°????????§???¢?????????
Polygon andrewScan(Polygon s) {
Polygon u, l;
if (s.size() < 3) return s;
sort(s.begin(), s.end());
u.push_back(s[0]);
u.push_back(s[1]);
l.push_back(s[s.size()-1]);
l.push_back(s[s.size()-2]);
// ??????????????¨?????????
for (int i = 2; i < s.size(); i++) {
for (int n = u.size(); n >= 2 && ccw(u[n-2], u[n-1], s[i]) != -1; n--) {
u.pop_back();
}
u.push_back(s[i]);
}
// ??????????????¨?????????
for (int i = s.size()-3; i >= 0; i--) {
for (int n = l.size(); n >= 2 && ccw(l[n-2], l[n-1], s[i]) != -1; n--) {
l.pop_back();
}
l.push_back(s[i]);
}
// ????¨????????????????????????????????????????????????????
reverse(l.begin(), l.end());
for (int i = u.size()-2; i >= 1; i--) l.push_back(u[i]);
return l;
}
// ????§???¢???????????????????????????3?????\???????????¶???
bool isConvex(Polygon s) {
int n = s.size();
int a;
for (int i = 0; i < n; i++) {
a = ccw(s[i], s[(i+1)%n], s[(i+2)%n]);
if (abs(a) == 1) break;
}
for (int i = 0; i < n; i++) {
int b = ccw(s[i], s[(i+1)%n], s[(i+2)%n]);
if (a*b < 0 && abs(b) == 1) {
return false;
}
}
return true;
}
int main() {
Polygon poly;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
poly.push_back(Vector(x, y));
}
if (isConvex(poly)) {
cout << "1" << endl;
}else {
cout << "0" << endl;
}
} |
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
// ------ Classes ------ //
class Point {
public:
long double px, py;
Point() : px(0), py(0) {};
Point(long double px_, long double py_) : px(px_), py(py_) {};
friend bool operator==(const Point& p1, const Point& p2) { return p1.px == p2.px && p1.py == p2.py; }
friend bool operator!=(const Point& p1, const Point& p2) { return p1.px != p2.px || p1.py != p2.py; }
friend bool operator<(const Point& p1, const Point& p2) { return p1.px < p2.px ? true : (p1.px == p2.px && p1.py < p2.py); }
friend bool operator>(const Point& p1, const Point& p2) { return p1.px > p2.px ? true : (p1.px == p2.px && p1.py > p2.py); }
friend bool operator<=(const Point& p1, const Point& p2) { return !(p1 > p2); }
friend bool operator>=(const Point& p1, const Point& p2) { return !(p1 < p2); }
friend Point operator+(const Point& p1, const Point& p2) { return Point(p1.px + p2.px, p1.py + p2.py); }
friend Point operator-(const Point& p1, const Point& p2) { return Point(p1.px - p2.px, p1.py - p2.py); }
friend Point operator*(const Point& p1, long double d) { return Point(p1.px * d, p1.py + d); }
friend Point operator*(long double d, const Point& p1) { return p1 * d; }
friend Point operator/(const Point& p1, long double d) { return Point(p1.px / d, p1.py / d); }
Point& operator+=(const Point& p1) { px += p1.px; py += p1.py; return *this; }
Point& operator-=(const Point& p1) { px -= p1.px; py -= p1.py; return *this; }
Point& operator*=(long double d) { px *= d; py *= d; return *this; }
Point& operator/=(long double d) { px /= d; py /= d; return *this; }
};
// ------ Functions ------ //
long double norm(const Point& a) { return a.px * a.px + a.py * a.py; }
long double abs(const Point& a) { return sqrtl(norm(a)); }
long double dot(const Point& a, const Point& b) { return a.px * b.px + a.py * b.py; }
long double crs(const Point& a, const Point& b) { return a.px * b.py - a.py * b.px; }
int ccw(Point p0, Point p1, Point p2) {
Point a = p1 - p0, b = p2 - p0;
if (crs(a, b) > 1e-10) return 1;
if (crs(a, b) < -1e-10) return -1;
if (dot(a, b) < -1e-10) return 2;
if (norm(a) < norm(b)) return -2;
return 0;
}
bool is_convex(vector<Point> v) {
if (v.size() < 3) return false;
int s = -3;
for (int i = 0; i < v.size(); i++) {
int r = ccw(v[i], v[(i + v.size() - 1) % v.size()], v[(i + 1) % v.size()]);
if (abs(r) == 1 && s == -3) s = r;
if (s == 1 && r == -1) return false;
if (s == -1 && r == 1) return false;
}
return true;
}
// ------ Main ------ //
int n; vector<Point> v;
int main() {
cin >> n; v.resize(n);
for (int i = 0; i < n; i++) cin >> v[i].px >> v[i].py;
cout << (is_convex(v) ? 1 : 0) << endl;
} |
#include<bits/stdc++.h>
using namespace std;
struct CWW{
CWW(){
cin.tie(0);
ios_base::sync_with_stdio(0);
cout<<fixed<<setprecision(15);
}
}STAR;
using D=double;
const D EPS=1e-8;
const D INF=1e9;
const int COUNTER_CLOCKWISE=1;
const int CLOCKWISE=-1;
const int ONLINE_FRONT=2;
const int ONLINE_BACK=-2;
const int ON_SEGMENT=0;
using Point=complex<D>;
struct Segment{
Point p1,p2;
Segment(const Point &p1=Point(),const Point &p2=Point()):p1(p1),p2(p2){}
};
struct Line{
Point p1,p2;
Line(const Point &p1=Point(),const Point &p2=Point()):p1(p1),p2(p2){}
};
struct Circle{
Point c;
D r;
Circle(const Point &c=Point(),const D &r=0.0):c(c),r(r){}
};
using Polygon=vector<Point>;
#define EQ(a,b) (abs((a)-(b))<EPS)
istream& operator>>(istream &is,Point &a){
D x,y;
is>>x>>y;
a=Point(x,y);
return is;
}
ostream& operator<<(ostream& os,const Point &a){
os<<real(a)<<" "<<imag(a);
return os;
}
istream& operator>>(istream &is,Line &l){
Point p1,p2;
is>>p1>>p2;
l=Line(p1,p2);
return is;
}
istream& operator>>(istream &is,Segment &s){
Point p1,p2;
is>>p1>>p2;
s=Segment(p1,p2);
return is;
}
D dot(const Point &a,const Point &b){
return real(a)*real(b)+imag(a)*imag(b);
}
D cross(const Point &a,const Point &b){
return real(a)*imag(b)-imag(a)*real(b);
}
Point projection(const Point &a,const Point &b){
return a*real(b/a);
}
Point projection(const Line &l,const Point &a){
return l.p1+projection(l.p2-l.p1,a-l.p1);
}
Point reflection(const Line &l,const Point &a){
Point p=projection(l,a);
return 2.0*p-a;
}
int ccw(Point a,Point b,Point c){
b-=a;c-=a;
if(cross(b,c)>EPS)return COUNTER_CLOCKWISE;
if(cross(b,c)<-EPS)return CLOCKWISE;
if(dot(b,c)<-EPS)return ONLINE_BACK;
if(norm(b)+EPS<norm(c))return ONLINE_FRONT;
return ON_SEGMENT;
}
bool isOrthogonal(const Point &a,const Point &b){
return EQ(dot(a,b),0.0);
}
bool isOrthogonal(const Point &a1,const Point &a2,const Point &b1,const Point &b2){
return isOrthogonal(a2-a1,b2-b1);
}
bool isOrthogonal(const Line &l,const Line &m){
return isOrthogonal(l.p1,l.p2,m.p1,m.p2);
}
bool isParallel(const Point &a,const Point &b){
return EQ(cross(a,b),0.0);
}
bool isParallel(const Point &a1,const Point &a2,const Point &b1,const Point &b2){
return isParallel(a1-a2,b1-b2);
}
bool isParallel(const Line &l,const Line &m){
return isParallel(l.p1,l.p2,m.p1,m.p2);
}
bool intersect(const Point &a1,const Point &a2,const Point &b1,const Point &b2){
return ccw(a1,a2,b1)*ccw(a1,a2,b2)<=0&&
ccw(b1,b2,a1)*ccw(b1,b2,a2)<=0;
}
bool intersect(const Segment &s,const Segment &t){
return intersect(s.p1,s.p2,t.p1,t.p2);
}
Point crossPoint(const Segment &s1,const Segment &s2){
Point base=s2.p2-s2.p1;
D d1=abs(cross(base,s1.p1-s2.p1));
D d2=abs(cross(base,s1.p2-s2.p1));
D t=d1/(d1+d2);
return s1.p1+(s1.p2-s1.p1)*t;
}
D distance(const Line &l,const Point &p){
return distance(projection(l,p),p);
}
D distance(const Line &l,const Line &m){
if(isParallel(l,m))return distance(l,m.p1);
return 0.0;
}
D distance(const Segment &s,const Point &p){
if(dot(s.p2-s.p1,p-s.p1)<0.0)return abs(p-s.p1);
if(dot(s.p1-s.p2,p-s.p2)<0.0)return abs(p-s.p2);
return distance(Line(s.p1,s.p2),p);
}
D distance(const Segment &s1,const Segment &s2){
if(intersect(s1,s2))return 0.0;
return min(min(distance(s1,s2.p1),distance(s1,s2.p2)),
min(distance(s2,s1.p1),distance(s2,s1.p2)));
}
D area(const Polygon &g){
D res=0.0;
for(int i=0;i<g.size();i++){
res+=cross(g[i],g[(i+1)%g.size()]);
}
return res/2.0;
}
bool isConvex(const Polygon &g){
for(int i=0;i<g.size();i++){
if(ccw(g[(i-1+g.size())%g.size()],g[i],g[(i+1)%g.size()])==CLOCKWISE)return false;
}
return true;
}
int main(){
int N;cin>>N;
Polygon g(N);
for(int i=0;i<N;i++)cin>>g[i];
cout<<isConvex(g)<<endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std ;
#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) begin(r),end(r)
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)
#define vvl(v,w,h,init) vector<vector<ll>> v(w,vector<ll>(h,init))
#define mp(a,b) make_pair(a,b)
#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))
using ll = long long;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;
template<typename T>
void O(T t){
cout << t << endl;
}
const double EPS = 1e-8;
const double INF = 1e12;
typedef complex<double> P;//????´???°????????¢?????????????????¨?§£???
namespace std {
bool operator < (const P& a, const P& b) {//x????????????
return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}
bool sorty(const P& a, const P& b) {//y????????????
return imag(a) != imag(b) ? imag(a) < imag(b) : real(a) < real(b);
}
}
double cross(const P& a, const P& b) {//??????
return imag(conj(a)*b);
}
double dot(const P& a, const P& b) {//??????
return real(conj(a)*b);
}
struct L : public vector<P> {//??´???
L(){};
L(const P &a, const P &b) {
push_back(a); push_back(b);
}
};
typedef vector<P> G;
struct C {//???
P p; double r;
C(){};
C(const P &p, double r) : p(p), r(r) { }
};
int ccw(P a, P b, P c) {//3????????????????????§????????§??????????????????
b -= a; c -= a;
if (cross(b, c) > 0) return +1; // counter clockwise
if (cross(b, c) < 0) return -1; // clockwise
if (dot(b, c) < 0) return +2; // c--a--b on line
if (norm(b) < norm(c)) return -2; // a--b--c on line
return 0;
}
bool is_convex(const G &g){
if(g.size() == 3) return true;
rep(i,g.size()){
if(ccw(g[i],g[(i+1) % g.size()],g[(i+2) % g.size()]) != 1 && ccw(g[i],g[(i+1) % g.size()],g[(i+2) % g.size()]) != -2){
return false;
}
}
return true;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
ll n;
double x,y;
cin >> n;
G g;
rep(i,n){
cin >> x >> y;
g.push_back({x,y});
}
cout << is_convex(g) << endl;
return 0;
} |
#include <cstdio> // printf(), scanf()
#define MAX_N 100
using namespace std;
static const int INF = 100000000;
static const double EPS = 1e-10;
static const int COUNTER_CLOCKWISE = 1;
static const int CLOCKWISE = -1;
static const int ONLINE_BACK = 2;
static const int ONLINE_FRONT = -2;
static const int ON_SEGMENT = 0;
class Point
{
public:
int x, y;
Point(int x = 0, int y = 0): x(x), y(y) {}
Point operator - (Point p) { return Point(x - p.x, y - p.y); }
};
typedef Point Vector;
Point ps[MAX_N];
int n;
double
norm(Vector a)
{
return a.x * a.x + a.y * a.y;
}
double
dot(Vector a, Vector b)
{
return a.x * b.x + a.y * b.y;
}
double
cross(Vector a, Vector b)
{
return a.x * b.y - a.y * b.x;
}
int
ccw(Point p0, Point p1, Point p2)
{
Vector a = p1 - p0;
Vector b = p2 - p0;
if (cross(a, b) > EPS)
return COUNTER_CLOCKWISE;
if (cross(a, b) < -EPS)
return CLOCKWISE;
if (dot(a, b) < -EPS)
return ONLINE_BACK;
if (norm(a) < norm(b))
return ONLINE_FRONT;
return ON_SEGMENT;
}
bool
solve()
{
if (n == 3)
return true;
int dir = INF;
int i = 0;
do {
int t = ccw(ps[i % n], ps[(i + 1) % n], ps[(i + 2) % n]);
i = (i + 1) % n;
if (dir == INF && (t == COUNTER_CLOCKWISE || t == CLOCKWISE))
{
dir = t;
continue;
}
if (t != ONLINE_FRONT && dir != t)
return false;
} while (i != 0);
return true;
}
int
main(int argc, char** argv)
{
scanf("%d", &n);
for (int i = 0; i < n; ++i)
scanf("%d %d", &ps[i].x, &ps[i].y);
printf("%d\n", solve() ? 1 : 0);
return 0;
} |
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
template<class T>
struct Vec2
{
Vec2(){}
Vec2(T _x, T _y) : x(_x), y(_y) {}
Vec2 operator+(const Vec2& rhs) const { return Vec2(x + rhs.x, y + rhs.y); }
Vec2 operator-(const Vec2& rhs) const { return Vec2(x - rhs.x, y - rhs.y); }
Vec2 operator*(T s) const { return Vec2(x*s, y*s); }
Vec2 operator/(T s) const { return Vec2(x/s, y/s); }
T dot(const Vec2& rhs) const { return x*rhs.x + y*rhs.y; }
T cross(const Vec2& rhs) const { return x*rhs.y - y*rhs.x; }
T sqlength() const { return x*x + y*y; }
double length() const { return sqrt(sqlength()); }
bool operator<(const Vec2& rhs) const
{
if (x != rhs.x) return x < rhs.x;
return y < rhs.y;
}
T x;
T y;
};
template<class T>
T cross(const Vec2<T> &O, const Vec2<T> &A, const Vec2<T> &B)
{
return (A - O).cross(B - O);
}
template<class T>
bool IsConvex(const vector<Vec2<T> >& poly)
{
for (size_t i = 0; i < poly.size() - 2; i++) {
if (cross(poly[i + 1], poly[i + 2], poly[i]) < 0 ) {
return false;
}
}
return true;
}
int main()
{
typedef Vec2<long long> Vec;
int n;
cin >> n;
vector<Vec> poly(n);
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
poly[i] = Vec(x, y);
}
poly.push_back(poly[0]);
poly.push_back(poly[1]);
cout << IsConvex(poly) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) { return (n==0) ? 1 : 10*TEN(n-1); }
template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;
template<class T> ostream& operator<<(ostream& os, const V<T> &v) {
os << "["; for (auto p: v) os << p << ", "; os << "]";
return os;
}
using D = double;
const D PI = acos(D(-1)), EPS = 1e-10;
int sgn(D a) { return (abs(a) <= EPS) ? 0 : (a < 0 ? -1 : 1); }
int sgn(D a, D b) { return sgn(a-b); }
//relative sign
// int rsgn(D a, D f) {
// if (abs(a) <= f*EPS) return 0;
// return (a < 0) ? -1 : 1;
// }
struct Pt2 {
D x, y;
Pt2() {}
Pt2(D _x, D _y) : x(_x), y(_y) {}
Pt2 operator+(const Pt2 &r) const { return Pt2(x+r.x, y+r.y); }
Pt2 operator-(const Pt2 &r) const { return Pt2(x-r.x, y-r.y); }
Pt2 operator*(const Pt2 &r) const { return Pt2(x*r.x-y*r.y, x*r.y+y*r.x); }
Pt2 operator*(const D &r) const { return Pt2(x*r, y*r); }
Pt2 operator/(const D &r) const { return Pt2(x/r, y/r); }
Pt2& operator+=(const Pt2 &r) { return *this=*this+r; }
Pt2& operator-=(const Pt2 &r) { return *this=*this-r; }
Pt2& operator*=(const Pt2 &r) { return *this=*this*r; }
Pt2& operator*=(const D &r) { return *this=*this*r; }
Pt2& operator/=(const D &r) { return *this=*this/r; }
Pt2 operator-() const { return Pt2(-x, -y); }
bool operator<(const Pt2 &r) const { return 2*sgn(x, r.x)+sgn(y, r.y)<0; }
bool operator==(const Pt2 &r) const { return sgn((*this-r).rabs()) == 0; }
D norm() const { return x*x + y*y; }
D abs() const { return sqrt(norm()); }
D rabs() const { return max(std::abs(x), std::abs(y)); } // robust abs
D arg() const { return atan2(y, x); }
pair<D, D> to_pair() const { return make_pair(x, y); }
static Pt2 polar(D le, D th) { return Pt2(le*cos(th), le*sin(th)); }
};
ostream& operator<<(ostream& os, const Pt2 &p) {
return os << "P(" << p.x << ", " << p.y << ")";
}
using P = Pt2;
struct L {
P s, t;
L() {}
L(P _s, P _t) : s(_s), t(_t) {}
P vec() const { return t-s; }
D abs() const { return vec().abs(); }
D arg() const { return vec().arg(); }
};
ostream& operator<<(ostream& os, const L &l) {
return os << "L(" << l.s << ", " << l.t << ")";
}
D cross(P a, P b) { return a.x*b.y - a.y*b.x; }
D dot(P a, P b) { return a.x*b.x + a.y*b.y; }
// cross(a, b) is too small?
int sgncrs(P a, P b) {
D cr = cross(a, b);
if (abs(cr) <= (a.rabs() + b.rabs()) * EPS) return 0;
return (cr < 0) ? -1 : 1;
}
// -2, -1, 0, 1, 2 : front, clock, on, cclock, back
int ccw(P b, P c) {
int s = sgncrs(b, c);
if (s) return s;
if (!sgn(c.rabs()) || !sgn((c-b).rabs())) return 0;
if (dot(b, c) < 0) return 2;
if (dot(-b, c-b) < 0) return -2;
return 0;
}
int ccw(P a, P b, P c) { return ccw(b-a, c-a); }
int ccw(L l, P p) { return ccw(l.s, l.t, p); }
using Pol = V<P>;
D area2(const Pol &pol) {
D u = 0;
P a = pol.back();
for (auto b: pol) u += cross(a, b), a = b;
return u;
}
// 0: outside, 1: on line, 2: inside
int contains(const Pol &pol, P p) {
int in = -1;
P _a, _b = pol.back();
for (int i = 0; i < int(pol.size()); i++) {
_a = _b; _b = pol[i];
P a = _a, b = _b;
if (ccw(a, b, p) == 0) return 1;
if (a.y > b.y) swap(a, b);
if (!(a.y <= p.y && p.y < b.y)) continue;
if (sgn(a.y, p.y) ? (cross(a-p, b-p) > 0) : (a.x > p.x)) in *= -1;
}
return in + 1;
}
Pol convex(Pol p) {
sort(begin(p), end(p));
p.erase(unique(begin(p), end(p)), end(p));
if (p.size() <= 1) return p;
Pol up, dw;
for (P d: p) {
size_t n;
while ((n = up.size()) > 1) {
// if (ccw(up[n-2], up[n-1], d) != 1) break; // line上も取る
if (ccw(up[n-2], up[n-1], d) == -1) break;
up.pop_back();
}
up.push_back(d);
}
reverse(begin(up), end(up));
for (P d: p) {
size_t n;
while ((n = dw.size()) > 1) {
// if (ccw(dw[n-2], dw[n-1], d) != -1) break; // line上も取る
if (ccw(dw[n-2], dw[n-1], d) == 1) break;
dw.pop_back();
}
dw.push_back(d);
}
dw.insert(begin(dw), begin(up) + 1, end(up) - 1);
return dw;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << setprecision(1) << fixed;
int n;
cin >> n;
Pol pol(n);
for (int i = 0; i < n; i++) {
D x, y;
cin >> x >> y; pol[i] = P(x, y);
}
P a, b = pol[pol.size()-2], c = pol.back();
for (int i = 0; i < n; i++) {
a = b; b = c; c = pol[i];
if (ccw(a, b, c) == -1) {
cout << 0 << endl;
return 0;
}
}
cout << 1 << endl;
return 0;
}
|
#include <iostream>
#include <vector>
#include <iomanip>
#include <cmath>
using namespace std;
const double EPS = 1e-8;
int dcmp(double d) {
if (fabs(d) < EPS) {
return 0;
}
else {
return d < 0? -1 : 1;
}
}
struct Vector {
double x, y;
Vector(double x = 0, double y = 0):x(x), y(y) {
}
};
typedef Vector Point;
Vector operator-(const Vector& a, const Vector& b) {
return Vector(a.x - b.x, a.y - b.y);
}
double cross(const Vector& a, const Vector& b) {
return a.x * b.y - a.y * b.x;
}
double ccw(const Point& a, const Point& b, const Point& c) {
return cross(b - a, c - a);
}
int main() {
int n;
cin >> n;
vector<Point> p(n);
for (int i = 0; i < n; i++) {
cin >> p[i].x >> p[i].y;
}
bool isConvex = true;
for (int i = 0; i < n - 1; i++) {
double area = ccw(p[i], p[(i + 1) % n], p[(i+ 2) % n]);
if (dcmp(area) < 0) {
isConvex = false;
break;
}
}
cout << (isConvex? 1 : 0) << endl;
return 0;
} |
#include<iostream>
#include<vector>
using namespace std;
struct Point { long double px, py; };
long double norm(const Point& a) { return a.px * a.px + a.py * a.py; }
long double dot(const Point& a, const Point& b) { return a.px * b.px + a.py * b.py; }
long double crs(const Point& a, const Point& b) { return a.px * b.py - a.py * b.px; }
Point Minus(Point a, Point b) { a.px -= b.px; a.py -= b.py; return a; }
int ccw(const Point& p0, const Point& p1, const Point& p2) {
Point a = Minus(p1, p0), b = Minus(p2, p0);
if (crs(a, b) > 1e-10) return 1;
if (crs(a, b) < -1e-10) return -1;
if (dot(a, b) < -1e-10) return 2;
if (norm(a) < norm(b)) return -2;
return 0;
}
bool is_convex(vector<Point> v) {
if (v.size() < 3) return false;
int s = -3;
for (int i = 0; i < v.size(); i++) {
int r = ccw(v[i], v[(i + v.size() - 1) % v.size()], v[(i + 1) % v.size()]);
if (abs(r) == 1 && s == -3) s = r;
if (s * r == -1) return false;
}
return true;
}
int n; Point p[1000]; vector<Point>G;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> p[i].px >> p[i].py; G.push_back(p[i]);
}
cout << is_convex(G) << endl;
return 0;
} |
#include<bits/stdc++.h>
#define lb long double
#define eps 1e-10
#define N 110
using namespace std;
struct Po{lb x,y;}a[N];
int n;
void get(Po &x){scanf("%Lf %Lf",&x.x,&x.y);}
Po operator -(Po x,Po y){return (Po){x.x-y.x,x.y-y.y};}
lb operator *(Po x,Po y){return x.x*y.y-x.y*y.x;}
int pa(int x){if (x<0)x+=n;if (x>=n)x-=n;return x;}
int main(){
scanf("%d",&n);
for (int i=0;i<n;i++)get(a[i]);
for (int i=0;i<n;i++)if ((a[pa(i+1)]-a[i])*(a[pa(i-1)]-a[i])<-eps){puts("0");return 0;}
puts("1");
return 0;
}
|
#include<bits/stdc++.h>
#define INTMAX 2147483647LL
#define PII pair<int,int>
#define MK make_pair
#define re register
#define Eps (1e-10)
#define Equal(a,b) (fabs((a)-(b))<Eps)
using namespace std;
typedef long long ll;
typedef double db;
const double Pi=acos(-1.0);
const int Inf=0x3f3f3f3f;
static const int COUNTER_CLOCKWISE = 1;
static const int CLOCKWISE = -1;
static const int ONLINE_BACK = 2;
static const int ONLINE_FRONT = -2;
static const int ON_SEGMENT = 0;
inline int read(){
re int x=0,f=1,ch=getchar();
while(!isdigit(ch))f=ch=='-'?-1:1,ch=getchar();
while(isdigit(ch))x=x*10+ch-48,ch=getchar();
return x*f;
}
inline ll readll(){
re ll x=0,f=1,ch=getchar();
while(!isdigit(ch))f=ch=='-'?-1:1,ch=getchar();
while(isdigit(ch))x=x*10+ch-48,ch=getchar();
return x*f;
}
struct Point{
db x,y;
Point(){}
Point(db xx,db yy):x(xx),y(yy){}
inline Point operator +(const Point &p){return Point(x+p.x,y+p.y);}
inline Point operator -(const Point &p){return Point(x-p.x,y-p.y);}
inline Point operator *(const db &k){return Point(x*k,y*k);}
inline Point operator /(const db &k){return Point(x/k,y/k);}
inline db Norm(){return x*x+y*y;}
inline db abs(){return sqrt(Norm());}
inline bool operator <(const Point &p)const{return x!=p.x?x<p.x:y<p.y;}
inline bool operator ==(const Point &p)const{return fabs(x-p.x)<Eps&&fabs(y-p.y)<Eps;}
};
typedef Point Vector;
typedef vector<Point> Polygon;
struct Segment{
Point p1,p2;
Segment(){}
Segment(Point p1_,Point p2_):p1(p1_),p2(p2_){}
};
typedef Segment Line;
struct Circle{
Point c;db r;
Circle(Point cc,db rr):c(cc),r(rr){}
};
double norm(Vector a){return a.x*a.x+a.y*a.y;}
double abs(Vector a){return sqrt(norm(a));}
double dot(Vector a,Vector b){return a.x*b.x+a.y*b.y;}
double cross(Vector a,Vector b){return a.x*b.y-a.y*b.x;}
inline bool Is_Vertical(Vector a,Vector b){return Equal(dot(a,b),0.0);}
inline bool Is_Vertical(Point a1,Point a2,Point b1,Point b2){return Is_Vertical(a1-a2,b1-b2);}
inline bool Is_Vertical(Segment s1,Segment s2){return Equal(dot(s1.p2-s1.p1,s2.p2-s2.p1),0.0);}
inline bool Is_Parallel(Vector a,Vector b){return Equal(cross(a,b),0.0);}
inline bool Is_Parallel(Point a1,Point a2,Point b1,Point b2){return Is_Parallel(a1-a2,b1-b2);}
inline bool Is_Parallel(Segment s1,Segment s2){return Equal(cross(s1.p2-s1.p1,s2.p2-s2.p1),0.0);}
Point Project(Segment s,Point p){
Vector bse=s.p2-s.p1;
double res=dot(p-s.p1,bse)/norm(bse);
return s.p1+bse*res;
}
Point Reflect(Segment s,Point p){return p+(Project(s,p)-p)*2.0;}
inline int CCW(Point p0,Point p1,Point p2){
Vector a=p1-p0,b=p2-p0;
if(cross(a,b)>Eps) return COUNTER_CLOCKWISE;
if(cross(a,b)<-Eps)return CLOCKWISE;
if(dot(a,b)<-Eps) return ONLINE_BACK;
if(a.Norm()<b.Norm()) return ONLINE_FRONT;
return ON_SEGMENT;
}
inline bool Is_Intersect(Point p1,Point p2,Point p3,Point p4){return (CCW(p1,p2,p3)*CCW(p1,p2,p4)<=0)&&(CCW(p3,p4,p1)*CCW(p3,p4,p2)<=0);}
inline bool Is_Intersect(Segment s1,Segment s2){return Is_Intersect(s1.p1,s1.p2,s2.p1,s2.p2);}
inline double Get_DistanceLP(Segment s,Point p){
return abs(cross(s.p2-s.p1,p-s.p1)/abs(s.p2-s.p1));
}
inline double Get_DistanceSP(Segment s,Point p){
if(dot(s.p2-s.p1,p-s.p1)<0.0) return abs(p-s.p1);
if(dot(s.p1-s.p2,p-s.p2)<0.0) return abs(p-s.p2);
return Get_DistanceLP(s,p);
}
inline double Get_Distance(Segment s1,Segment s2){
if(Is_Intersect(s1,s2)) return 0.0;
return min(min(Get_DistanceSP(s1,s2.p1),Get_DistanceSP(s1,s2.p2)),min(Get_DistanceSP(s2,s1.p1),Get_DistanceSP(s2,s1.p2)));
}
inline double Get_Distance(Point p1,Point p2,Point p3,Point p4){return Get_Distance(Segment(p1,p2),Segment(p3,p4));}
inline Point Get_Crosspoint(Segment s1,Segment s2){
Vector bse=s2.p2-s2.p1;
double d1=abs(cross(bse,s1.p1-s2.p1));
double d2=abs(cross(bse,s1.p2-s2.p1));
double t=d1/(d1+d2);
return s1.p1+(s1.p2-s1.p1)*t;
}
inline Point Get_Crosspoint(Point p1,Point p2,Point p3,Point p4){return Get_Crosspoint(Segment(p1,p2),Segment(p3,p4)); }
inline double Area(Polygon s){
double res=0;
for(int i=0;i<(int)s.size();++i)
res+=cross(s[i],s[(i+1)%(int)s.size()])/2.0;
return abs(res);
}
inline bool Is_Convex(Polygon p){
bool f=true;int n=p.size();
for(int i=0;i<n;++i){
int t=CCW(p[(i+n-1)%n],p[i],p[(i+1)%n]);
f&=t!=CLOCKWISE;
}
return f;
}
int n;
int main(){
n=read();
Polygon p(n);
for(int i=0;i<n;++i)
p[i].x=read(),p[i].y=read();
printf("%d\n",Is_Convex(p));
return 0;
}
|
#include <bits/stdc++.h>
#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)
#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=int(a)-1;i>=int(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)
#define _all(arg) begin(arg),end(arg)
#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))
#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)
#define clr(a,b) memset((a),(b),sizeof(a))
#define bit(n) (1LL<<(n))
#define popcount(n) (__builtin_popcountll(n))
using namespace std;
template<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}
template<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}
using ll=long long;
using R=long double;
const R EPS=1e-9L; // [-1000,1000]->EPS=1e-8 [-10000,10000]->EPS=1e-7
inline int sgn(const R& r){return(r > EPS)-(r < -EPS);}
inline R sq(R x){return sqrt(max(x,0.0L));}
const int dx[8]={1,0,-1,0,1,-1,-1,1};
const int dy[8]={0,1,0,-1,1,1,-1,-1};
// Problem Specific Parameter:
// Description: ????????????
// Verifyed: various problem
const R INF = 1E40L;
const R PI = acos(-1.0L);
using P=complex<R>;
const P O=0.0L;
using L=struct{P s,t;};
using VP=vector<P>;
using C=struct{P p;R c;};
istream& operator >> (istream& is,P& p){ R x,y;is >> x >> y; p=P(x,y); return is;}
ostream& operator << (ostream& os,P& p){ os << real(p) << " " << imag(p); return os;}
inline R dot(P o,P a,P b){return real(conj(a-o)*(b-o));}
inline R det(P o,P a,P b){return imag(conj(a-o)*(b-o));}
inline int sdot(P o,P a,P b){return sgn(dot(o,a,b));}
inline int sdet(P o,P a,P b){return sgn(det(o,a,b));}
//projection verify AOJ CGL_1_A
P proj(L l,P p){ R u=real((p-l.s)/(l.t-l.s)); return (1-u)*l.s+u*l.t;}
// vertical parallel
// verified: AOJ CGL_2_A
bool vertical(L a,L b) {return sdot(O,a.t-a.s,b.t-b.s)==0;}
bool parallel(L a,L b) {return sdet(O,a.t-a.s,b.t-b.s)==0;}
bool eql(L a,L b){ return parallel(a,b) and sdet(a.s,a.t,b.s)==0;}
// crossing determination
// verified: AOJ CGL_2_B
bool iss(L a,L b){
int sa=sdet(a.s,a.t,b.s)*sdet(a.s,a.t,b.t);
int sb=sdet(b.s,b.t,a.s)*sdet(b.s,b.t,a.t);
return max(sa,sb)<0;
}
// crossing point
// verified: AOJ CGL_2_C
P cross(L a,L b){
R u=det(a.s,b.s,b.t)/det(O,a.t-a.s,b.t-b.s);
return (1-u)*a.s+u*a.t;
}
// distance
// verified: AOJ CGL_2_D
R dsp(L l,P p){
P h=proj(l,p);
if(sdot(l.s,l.t,p)<=0) h=l.s;
if(sdot(l.t,l.s,p)<=0) h=l.t;
return abs(p-h);
}
R dss(L a,L b){return iss(a,b)?0:min({dsp(a,b.s),dsp(a,b.t),dsp(b,a.s),dsp(b,a.t)});}
// Polygon
// area
// verified: AOJ 1100 CGL_3_A
R area(const VP& pol){
int n=pol.size();
R sum=0.0;
rep(i,n) sum+=det(O,pol[i],pol[(i+1)%n]);
return abs(sum/2.0L);
}
// convex_polygon determination
// verified: CGL_3_B
bool is_convex(const VP& pol){
int n=pol.size();
rep(i,n)if(sdet(pol[i],pol[(i+1)%n],pol[(i+2)%n])<0) return false;
return true;
}
/*
// polygon realation determination in 2 on 1 out 0???(possible non-convex)
// verified: AOJ CGL_3-C
int in_polygon(const VP& pol, const P& p){
int n=pol.size(),res=0;
rep(i,n){
P a=pol[i],b=pol[(i+1)%n];
if(sdot(p,a,b)<0) return 1;
bool f=sgn(imag(p-a))>=0,s=sgn(imag(p-b))<0;
if(sgn(imag(b-a))*sdet(a,b,p)==1 and f==s) res+=(2*f-1);
}
return res?2:0;
}
// polygon realation determination???(possible non-convex)
// verified: AOJ 2514
bool in_polygon(const VP& pol, const L& l){
VP check{l.s,l.t};
int n=pol.size();
rep(i,n){
L edge={pol[i],pol[(i+1)%n]};
if(iss(l,edge)) check.emplace_back(cross(l,edge));
}
sort(begin(check),end(check),[](P a,P b)->bool{return sgn(real(a)-real(b))?:sgn(imag(a)-imag(b));});
n=check.size();
rep(i,n-1){
P m=(check[i]+check[i+1])/2.0L;
if(in_polygon(pol,m)==false) return false;
}
return true;
}
// convex_cut
// verified: AOJ CGL_4_C
VP convex_cut(const VP& pol,const L& l) {
VP res;
int n=pol.size();
rep(i,n){
P a = pol[i],b=pol[(i+1)%n];
int da=sdet(l.s,l.t,a),db=sdet(l.s,l.t,b);
if(da>=0) res.emplace_back(a);
if(da*db<0) res.emplace_back(cross({a,b},l));
}
return res;
}
*/
int main(void){
int n;
cin >> n;
VP pol;
rep(i,n){
P p;
cin >> p;
pol.push_back(p);
}
cout << is_convex(pol) << endl;
return 0;
} |
#include <iostream>
#include <vector>
using namespace std;
template <class T> void readVal(vector<T>& vec, int idx) {
T input;
std::cin >> input;
vec[idx] = input;
}
int main()
{
char ans = '1';
int size;
cin >> size;
vector<int> x(size);
vector<int> y(size);
int dir(0), vecx(0), vecy(0);
for (int i = 0; i < size; i++) {
readVal(x, i);
readVal(y, i);
}
for (int i = 0; i <= size; i++) {
int new_vecx = x[(i + 1) % size] - x[i % size];
int new_vecy = y[(i + 1) % size] - y[i % size];
int cross_prod = (vecx * new_vecy - vecy * new_vecx);
if (cross_prod != 0) {
if (cross_prod * dir >= 0) {
dir = cross_prod > 0 ? 1 : -1;
}
else {
ans = '0';
break;
}
}
vecx = new_vecx;
vecy = new_vecy;
}
cout << ans << "\n";
return 0;
}
|
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
inline double add(double a, double b){
return abs(a+b)<(1e-10)*(abs(a)+abs(b)) ? 0.0 : a+b;
}
struct vec{
double x,y;
vec operator-(vec b){
return (vec){add(x,-b.x),add(y,-b.y)};
}
vec operator+(vec b){
return (vec){add(x,b.x),add(y,b.y)};
}
vec operator*(double d){
return (vec){x*d,y*d};
}
double dot(vec v){
return add(x*v.x,y*v.y);
}
double cross(vec v){
return add(x*v.y,-y*v.x);
}
double norm(){
return sqrt(x*x+y*y);
}
};
typedef vector<vec> polygon;
int ccw(vec& a, vec& b, vec& c){
vec ab = b-a, ac = c-a;
double o = ab.cross(ac);
if(o>0) return 1; //CCW
if(o<0) return -1; //CW
if(ab.dot(ac)<0){
return 2; //C-A-B
}else{
if(ab.dot(ab)<ac.dot(ac)){
return -2; //A-B-C
}else{
return 0; //A-C-B
}
}
}
bool isConvex(polygon& g){
int n=g.size()-2;
for(int i=0;i<n;i++){
if(!~ccw(g[i],g[i+1],g[i+2]))return false;
}
return true;
}
int main(){
int n;
cin >> n;
polygon g;
vec v;
while(n--){
cin >> v.x >> v.y;
g.push_back(v);
}
g.push_back(g[0]);
g.push_back(g[1]);
cout.precision(1);
cout << fixed;
cout << (isConvex(g)?1:0) << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define E 1e-18
#define ll long long
#define int long long
#define db double
#define For(i,j,k) for(int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for(int i=(int)(j);i>=(int)(k);i--)
inline ll read(){
ll x=0;char ch=getchar();bool f=0;
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=1;
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
return f?-x:x;
}
void write(ll x){
if(x<0) putchar('-'),x=-x;
if(x>=10) write(x/10);putchar(x%10+'0');
}
void writeln(ll x){write(x);puts("");}
void writep(ll x){write(x);putchar(' ');}
int const N=100+3;
struct point{
db x,y;
point operator - (point A) const{return (point){x-A.x,y-A.y};}
point operator + (point A) const{return (point){x+A.x,y+A.y};}
void print(){printf("%.10lf %.10lf\n",x,y);}
}A[N];
point operator *(db x,point A){return (point){x*A.x,x*A.y};}
db CJ(point A,point B){return A.x*B.y-B.x*A.y;}//叉积
db DJ(point A,point B){return A.x*B.x+A.y*B.y;}//点积
db dis(point A){return sqrt(A.x*A.x+A.y*A.y);}//向量长度
bool cmp(point A,point B){return (CJ(A,B)>0 || CJ(A,B)==0 && dis(A)<dis(B));}//按极角排序
point CGL_1_A(point A,point B,point C){//点在直线上的投影点
db d=dis(B-A);
return (A+(DJ(C-A,B-A)/d/d*(B-A)));
}
point CGL_1_B(point A,point B,point C){//点关于直线的对称点
point P=CGL_1_A(A,B,C);
return (C+2*(P-C));
}
int CGL_1_C(point A,point B,point C){//向量的位置关系(向量AC和向量AB)
if (CJ(B-A,C-A)>0) return 1;//逆时针
if (CJ(B-A,C-A)<0) return 2;//顺时针
if (DJ(C-A,B-A)<0) return 3;//方向相反
if (dis(C-A)>dis(B-A)) return 4;//方向相同 AC>AB
if (dis(C-A)<=dis(B-A)) return 5;//方向相同 AB>AC
}
int CGL_2_A(point A,point B,point C,point D){//判断直线平行、垂直
if (CJ(B-A,D-C)==0) return 2;//平行
if (DJ(B-A,D-C)==0) return 1;//垂直
return 0;//相交
}
int CGL_2_B(point A,point B,point C,point D){//判断线段是否有交
if (min(A.x,B.x)>max(C.x,D.x) || min(C.x,D.x)>max(A.x,B.x) || min(A.y,B.y)>max(C.y,D.y) || min(C.y,D.y)>max(A.y,B.y)) return 0;//快速排斥实验(两个矩形是否相交)
return CJ(B-A,C-A)*CJ(B-A,D-A)<=0 && CJ(D-C,A-C)*CJ(D-C,B-C)<=0;//跨立实验
}
point CGL_2_C(point A,point B,point C,point D){//求直线交点
point p1=CGL_1_A(A,B,C),p2=CGL_1_A(A,B,D);//求C、D在AB上的投影点
db d1=dis(C-p1),d2=dis(D-p2);//三角形的相似比
if (CJ(B-A,C-A)*CJ(B-A,D-A)<0) return (C+(d1/(d1+d2)*(D-C)));
else if (d1<d2) return (D+d2/(d2-d1+E)*(C-D));
else return (C+d1/(d1-d2+E)*(D-C));
}
db Distance(point A,point B,point C){//点到线段的距离
point P=CGL_1_A(A,B,C);//C在AB上的投影点
if (P.x>=min(A.x,B.x) && P.x<=max(A.x,B.x) && P.y>=min(A.y,B.y) && P.y<=max(A.y,B.y)) return dis(P-C);//投影点在线段上
return min(dis(A-C),dis(B-C));//两个端点到C的最小值
}
db CGL_2_D(point A,point B,point C,point D){//线段到线段的距离
point P=CGL_2_C(A,B,C,D);//求直线交点
if (P.x>=min(A.x,B.x) && P.x<=max(A.x,B.x) && P.y>=min(A.y,B.y) && P.y<=max(A.y,B.y) && P.x>=min(C.x,D.x) && P.x<=max(C.x,D.x) && P.y>=min(C.y,D.y) && P.y<=max(C.y,D.y)) return 0;//线段有交点
return min(Distance(A,B,C),min(Distance(A,B,D),min(Distance(C,D,A),Distance(C,D,B))));
}
db CGL_3_A(){//多边形的有向面积
int n=read(),ans=0;
for (int i=1;i<=n;i++) scanf("%lf%lf",&A[i].x,&A[i].y);
A[n+1]=A[1];
for (int i=1;i<=n;i++) ans+=CJ(A[i],A[i+1]);//叉积求平行四边形面积
return ans/2.0;//总面积要 ÷2
}
int CGL_3_B(){//判断多边形是否为凸多边形
int n=read();
for (int i=1;i<=n;i++) scanf("%lf%lf",&A[i].x,&A[i].y);
A[0]=A[n];A[n+1]=A[1];
for (int i=1;i<=n;i++) if (CJ(A[i-1]-A[i],A[i+1]-A[i])>0) return 0;
return 1;
}
signed main(){
writeln(CGL_3_B());
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a, i##end = b; i <= i##end; ++i)
#define per(i, a, b) for (int i = a, i##end = b; i >= i##end; --i)
#define rep0(i, a) for (int i = 0, i##end = a; i < i##end; ++i)
#define per0(i, a) for (int i = (int)a-1; ~i; --i)
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define chkmax(a, b) a = max(a, b)
#define chkmin(a, b) a = min(a, b)
#define x first
#define y second
#define enter putchar('\n')
typedef long long ll;
typedef double DB;
const DB eps = 1e-12;
int sgn(DB x) { return fabs(x) < eps ? 0 : (x > 0 ? 1 : -1); }
struct Point {
DB x, y;
Point(DB x = 0, DB y = 0) : x(x), y(y) {}
Point operator + (Point a) { return Point(x + a.x, y + a.y); }
Point operator - (Point a) { return Point(x - a.x, y - a.y); }
Point operator - () { return Point(-x, -y); }
friend Point operator * (DB k, Point a) { return Point(k * a.x, k * a.y); }
DB operator % (Point a) { return x * a.x + y * a.y; }
DB operator / (Point a) { return x * a.y - y * a.x; }
operator DB() { return sqrt(x*x + y*y); }
};
Point gp() { Point a; scanf("%lf%lf", &a.x, &a.y); return a; }
void wp(Point a) { printf("%.10lf %.10lf ", a.x, a.y); }
struct Poly {
std::vector<Point> a;
Poly(int n = 0) { a.resize(n); rep0(i, n) a[i] = gp(); }
int size() { return a.size(); }
Point &operator [] (int i) { int n = size(); return a[i >= n ? i-n : i]; }
};
DB Area(Poly A) {
DB res = 0;
rep(i, 2, A.size()-1) res += (A[i]-A[0])/(A[i-1]-A[0]);
return fabs(res)/2;
}
int Direct2(Point P, Point P1, Point P2) { return sgn((P1-P)/(P2-P)); }
bool IsConvex(Poly A) {
bool f1 = 1, f2 = 1;
rep0(i, A.size()) {
int t = Direct2(A[i], A[i+1], A[i+2]);//sgn((A[i+1]-A[i])/(A[i+2]-A[i+1]));
t && (t == 1 ? f1 = 0 : f2 = 0);
}
return f1 || f2;
}
int q;
int main() {
scanf("%d", &q);
printf("%d\n", IsConvex(Poly(q)));
/*
scanf("%d", &q);
while (q--) {
Point P1 = gp(), P2 = gp(), Q1 = gp(), Q2 = gp();
printf("%.10lf\n", SegDist(P1, P2, Q1, Q2));
//wp(GetSegInter(P1, P2, Q1, Q2)); enter;
//printf("%d\n", IsInter(P1, P2, Q1, Q2));
//wp(Proj(P, P1, P2)); enter;
}*/
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10;
bool dless(double d1, double d2) { return d1 < d2 + eps; }
bool dcmp(double d, double d2 = 0) { return abs(d - d2) < eps; }
double sgn(double d) {
if (dcmp(d))
return 0;
if (d > 0)
return 1;
return -1;
}
struct vec {
double x, y;
vec(double x, double y) : x(x), y(y) {}
vec() : x(0), y(0) {}
const bool operator==(const vec &v) const {
return dcmp(x, v.x) && dcmp(y, v.y);
}
const bool operator!=(const vec &v) const { return !(*this == v); }
const vec operator+(const vec &v) const { return vec(x + v.x, y + v.y); }
const vec operator-() const { return vec(-x, -y); }
const vec operator-(const vec &v) const { return *this + (-v); }
const vec operator*(const double d) const { return vec(x * d, y * d); }
const vec operator/(const double d) const { return *this * (1 / d); }
const vec unit() const { return *this / len(); }
const double len() const { return sqrt(x * x + y * y); }
const double dot(const vec &v) const { return x * v.x + y * v.y; }
const double cross(const vec &v) const { return x * v.y - y * v.x; }
const bool parallel(const vec &v) const { return dcmp(cross(v)); }
const bool perpendicular(const vec &v) const { return dcmp(dot(v)); }
};
typedef vec point;
template <class T> struct optional {
bool has;
T val;
optional() : has(false) {}
optional(T t) : has(true), val(t) {}
};
struct line {
double a, b, c;
line(point p1, point p2) {
a = p1.y - p2.y;
b = p2.x - p1.x;
c = (p1.x - p2.x) * p1.y + (p2.y - p1.y) * p1.x;
double m1 = a * p1.x + b * p1.y + c;
double m2 = a * p2.x + b * p2.y + c;
}
bool parallel(line l) { return dcmp(a * l.b, b * l.a); }
optional<point> intersect(line l) {
if (parallel(l))
return optional<point>();
return point(-(c * l.b - b * l.c) / (a * l.b - b * l.a),
(c * l.a - a * l.c) / (a * l.b - b * l.a));
}
bool contains(point p) { return dcmp(a * p.x + b * p.y + c); }
point project(point p) {
double t = -(a * p.x + b * p.y + c) / (a * a + b * b);
return point(p.x + a * t, p.y + b * t);
}
};
struct segment {
point p1, p2;
segment(point p1, point p2) : p1(p1), p2(p2) {}
line sline() { return line(p1, p2); }
bool contains(point p) {
double x1 = min(p1.x, p2.x), x2 = max(p1.x, p2.x);
double y1 = min(p1.y, p2.y), y2 = max(p1.y, p2.y);
bool ok = sline().contains(p) && (dless(x1, p.x) && dless(p.x, x2)) &&
(dless(y1, p.y) && dless(p.y, y2));
return ok;
}
bool intersect(segment s) {
vec v0 = p2 - p1;
vec v1 = s.p2 - s.p1;
if (v0.parallel(v1)) {
return (contains(s.p1) || contains(s.p2)) ||
(s.contains(p1) || s.contains(p2));
}
point a = p1, b = p2, c = s.p1, d = s.p2;
return sgn((a - b).cross(b - c)) != sgn((a - b).cross(b - d)) &&
sgn((c - d).cross(d - a)) != sgn((c - d).cross(d - b));
}
double distance(point p) {
point p0 = sline().project(p);
if (contains(p0)) {
return (p0 - p).len();
} else {
return min((p1 - p).len(), (p2 - p).len());
}
}
double distance(segment s) {
if (intersect(s))
return 0.0;
return min(min(distance(s.p1), distance(s.p2)),
min(s.distance(p1), s.distance(p2)));
}
};
point p[105];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> p[i].x >> p[i].y;
p[n] = p[0];
int f[3] = {0, 0, 0};
for (int i = 0; i < n; i++) {
int d = sgn((p[i + 2] - p[i + 1]).cross(p[i + 1] - p[i]));
f[d + 1]++;
}
cout << (f[0] == 0 || f[2] == 0) << endl;
}
|
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <bitset>
#include <deque>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
#include <chrono>
#include <random>
#include <tuple>
#include <utility>
#include <fstream>
#include <complex>
const long INF = (1l << 30);
const long LINF = (1l << 60);
//geometric library
//#include <complex>
typedef std::complex<double> Com;
//内積
double dot_product(const Com a, const Com b){
return (conj(a)*b).real();
}
//外積
double cross_product(const Com a, const Com b){
return (conj(a)*b).imag();
}
//点と直線の距離
double dist_dot_line(const Com st, const Com en, const Com dt){
return std::abs(cross_product(dt-st, en-st) / std::abs(en-st));
}
//点と線分の距離
double dist_dot_seg(const Com st, const Com en, const Com dt){
if(dot_product(en-st, dt-st) <= 0){
return std::abs(dt-st);
}else if(dot_product(st-en, dt-en) <= 0){
return std::abs(dt-en);
}else{
return dist_dot_line(st, en, dt);
}
}
//線分の交差判定
//平行・接触は含まない(接触を含みたいときはdist_seg==0を使う)
bool seg_crossing(const Com st1, const Com en1, const Com st2, const Com en2){
double cross1 = cross_product(en1-st1, st2-st1) * cross_product(en1-st1, en2-st1);
double cross2 = cross_product(en2-st2, st1-st2) * cross_product(en2-st2, en1-st2);
return (cross1 < 0) && (cross2 < 0);
}
//線分と線分の距離
double dist_seg(const Com st1, const Com en1, const Com st2, const Com en2){
if(seg_crossing(st1, en1, st2, en2)){
return 0;
}
return std::min(std::min(dist_dot_seg(st1, en1, st2), dist_dot_seg(st1, en1, en2)), std::min(dist_dot_seg(st2, en2, st1), dist_dot_seg(st2, en2, en1)));
}
//線分と線分の垂直判定
bool is_vertical(const Com st1, const Com en1, const Com st2, const Com en2){
if(dot_product(en1-st1, en2-st2) == 0){
return true;
}else{
return false;
}
}
//平行判定
bool is_parallel(const Com st1, const Com en1, const Com st2, const Com en2){
if(cross_product(en1-st1, en2-st2) == 0){
return true;
}else{
return false;
}
}
//直線・線分の交点
Com cross_point(const Com st1, const Com en1, const Com st2, const Com en2){
/* 線分の時はコメントを外した方が良さそう
if(dist_seg(st1, en1, st2, en2) > 0 || is_parallel(st1, en1, st2, en2)){
printf("Error! cross_point invalid input\n");
std::exit(EXIT_FAILURE);
}
//*/
double den = (real(st1)-real(en1)) * (imag(en2)-imag(st2)) - (real(en2)-real(st2)) * (imag(st1)-imag(en1));
double mol = (real(en2)-real(en1)) * (imag(en2)-imag(st2)) - (real(en2)-real(st2)) * (imag(en2)-imag(en1));
double s = mol/den;
return st1 * s + en1 * (1-s);
}
long n;
Com p[105];
int main(){
scanf("%ld", &n);
for(int i = 0; i < n; i++){
double x, y;
scanf("%lf%lf", &x, &y);
p[i] = Com(x, y);
}
p[n] = p[0], p[n+1] = p[1];
for(int i = 0; i < n; i++){
if(cross_product(p[i+1]-p[i], p[i+2]-p[i+1]) < 0){
printf("0\n");
return 0;
}
}
printf("1\n");
}
|
#include<bits/stdc++.h>
using namespace std;
#define EPS 1e-10
#define equals(a,b) (fabs( (a) - (b) )< EPS )
// c++ 11,14
typedef struct point{
double x,y;
point(){};
point(double x ,double y):x(x),y(y){};
point operator + (point &p){ return point(x+p.x,y+p.y); }
point operator - (point &p){ return point(x-p.x,y-p.y); }
point operator * (point &p){ return point(x*p.x-y*p.y,x*p.y+y*p.x) ;}
point operator * (double a){ return point(x*a,y*a); }
point operator / (double a){ return point(x/a,y/a); }
double abs() { return sqrt(norm()); }
double norm() { return x*x+y*y; }
bool operator < (const point &p) const { return x!=p.x ? x<p.x : y<p.y; }
bool operator == (const point &p) const { return fabs(x-p.x)<EPS && fabs(y-p.y)<EPS; }
}point;
double abs(point a){return a.abs();}
double norm(point a){return a.norm();}
typedef complex<double> C;
typedef struct { point s,e;} line;
C convert(point a){ return C(a.x,a.y); }
point convert( C a){ return point(a.real(),a.imag() );}
double dot(point a,point b){ return a.x*b.x+a.y*b.y ; } //内積 a・b
double cross(point a,point b){ return a.x*b.y - a.y*b.x ; }//外積(z成分) a×b
point vec(line l){return l.e-l.s;}
line make(point s,point e){
line res; res.s=s; res.e=e;
return res;
}
point make(){
double x,y; cin>>x>>y;
return point(x,y);
}
//直交
bool isorthogonal(point a,point b){ return equals(dot(a,b), 0.0); }
bool isorthogonal(line l1,line l2){ return isorthogonal(vec(l1),vec(l2)); }
//平行
bool isparallel(point a,point b){ return equals(cross(a,b),0.0); }
bool isparallel(line l1,line l2){ return isparallel(vec(l1),vec(l2)); }
//射影
point project(line s,point p){
point base = vec(s);
double r=dot(p-s.s,base)/base.norm();
base = base*r;
return s.s+base;
}
//反射
point reflect(line l,point p){
point tmp=project(l,p)-p;
tmp= tmp*2.0;
return p+tmp;
}
//交差判定
int ccw(point p0,point p1,point p2){
point a = p1-p0;
point b = p2-p0;
if(cross(a,b)>EPS) return 1;//counter_clockwise
if(cross(a,b)<-EPS) return -1;//clockwise
if(dot(a,b)<-EPS)return 2;//online_back
if(a.norm()<b.norm() ) return -2;//online_front
return 0;//on_segment
}
// line p1-p2 line p3-p4
bool intersect(point p1,point p2,point p3,point p4){
return (ccw(p1,p2,p3)*ccw(p1,p2,p4)<=0 && ccw(p3,p4,p1)*ccw(p3,p4,p2)<=0 );
}
// line l1,l2
bool intersect(line l1,line l2){ return intersect(l1.s,l1.e,l2.s,l2.e); }
//距離
//point-point
double distance(point a,point b){ return abs(a-b); }
//point-line(直線)
double distance2(line l,point p){
return abs(cross(vec(l),p-l.s)/abs(l.e-l.s));
}
//point-line(線分)
double distance(line l,point p){
if( dot(vec(l),p-l.s) <0.0 ) return abs(p-l.s);
if( dot(l.s-l.e,p-l.e) <0.0 ) return abs(p-l.e);
return distance2(l,p);
}
//line-line
double distance(line l1,line l2){
if(intersect(l1,l2)) return 0.0;
return min(min(distance(l1,l2.s),distance(l1,l2.e) ), min(distance(l2,l1.s),distance(l2,l1.e) ) );
}
//交点
point crosspoint(line l1,line l2){
point base = vec(l2);
double d1 = abs(cross(base,l1.s-l2.s) );
double d2 = abs(cross(base,l1.e-l2.s) );
double t = d1/(d1+d2);
point tmp = vec(l1)*t;
return l1.s+tmp;
}
int main(){
double ans=0;
int n;
cin>>n;
vector<point> p(n);
for(int i=0;i<n;i++){
p[i]=make();
}
bool flag=true;
ans=cross(p[2]-p[1],p[1]-p[0]);
//cout<<ans<<endl;
for(int i=1;i<n-2;i++){
double tmp=cross(p[i+2]-p[i+1],p[i+1]-p[i]);
if(ans==0.0)ans=tmp;
//cout<<tmp<<endl;
if(ans*tmp<0){
cout<<0<<endl;
flag=false;
break;
}
}
double tmp=cross(p[0]-p[n-1],p[n-1]-p[n-2]);
//cout<<tmp<<endl;
if(flag&&ans*tmp<0){
cout<<0<<endl;
flag=false;
}
if(flag)cout<<1<<endl;
return 0;
}
|
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int INF=0x3f3f3f3f;
const int N = 210;
struct node
{
int x ,y;
node(){}
node(int x1 ,int y1):x(x1),y(y1){}
bool operator < (const node &a)const{
if(x!=a.x)
return x < a.x;
else
return y < a.y;
}
}point[N];
struct Line
{
node p1 , p2;
Line(){}
Line(node x , node y):p1(x),p2(y){}
}line[N];
bool judge(Line L1 , Line L2)
{
int x1 , y1 , x2 , y2;
x1=L1.p2.x - L1.p1.x;
y1=L1.p2.y - L1.p1.y;
x2=L2.p2.x - L2.p1.x;
y2=L2.p2.y - L2.p1.y;
///cout<<x1*y2-y1*x2<<endl;
if(x1 * y2 - y1*x2 >= 0)
return true;
else
return false;
}
int main()
{
int n;
while(scanf("%d", &n)!=EOF){
int x , y;
for(int i=1;i<=n;i++){
scanf("%d%d", &x, &y);
point[i]=node(x , y);
}
int m = 0;
for(int i=1;i<=n;i++){
if(i==n)
line[m++]=Line(point[n] , point[1]);
else
line[m++]=Line(point[i] , point[i+1]);
}
bool p=true;
for(int i=0;i<m;i++){
if(i==m-1){
if(!judge(line[i] , line[0])){
p=false;
break;
}
}
else if(!judge(line[i] , line[i+1])){
p=false;
break;
}
}
printf("%d\n",p ? 1 : 0);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10;
int dcmp(double x) {
if(fabs(x) < eps) return 0;
return x < 0 ? -1 : 1;
}
bool same(double a, double b) {return dcmp(a - b) == 0;}
#define Vector P
struct P {
double x, y;
P(double x = 0, double y = 0): x(x), y(y) {}
P operator + (P b) {return P(x + b.x, y + b.y);}
P operator - (P b) {return P(x - b.x, y - b.y);}
P operator * (double b) {return P(x * b, y * b);}
P operator / (double b) {return P(x / b, y / b);}
double operator * (P b) {return x * b.x + y * b.y;} // Dot
double operator ^ (P b) {return x * b.y - y * b.x;} // Cross
double abs() {return hypot(x, y);}
P unit() {return *this / abs();}
P spin(double o) {
double c = cos(o), s = sin(o);
return P(c * x - s * y, s * x + c * y);
}
};
struct Line {
//ax + by + c = 0
double a, b, c, theta;
P pa, pb;
Line(): a(0), b(0), c(0), theta(0), pa(), pb() {}
Line(P pa, P pb): a(pa.y - pb.y), b(pb.x - pa.x), c(pa ^ pb), theta(atan2(-a, b)), pa(pa), pb(pb) {} P projection(P p) {return pa + (pb - pa).unit() * ((pb - pa) * (p - pa) / (pb - pa).abs());} P reflection(P p) {return p + (projection(p) - p) * 2;}
double get_ratio(P p) {return (p - pa) * (pb - pa) / ((pb - pa).abs() * (pb - pa).abs());}
P dis(P p) {return ((pb - pa) ^ (p - pa)) / (pb - pa).abs();} // directed distance
};
struct Circle {
P c;
double r;
Circle(P c, double r = 0): c(c), r(r) {}
};
bool onsegment(P p, P a, P b) {
return dcmp((a - p) ^ (b - p)) == 0 && dcmp((a - p) * (b - p)) <= 0;
}
bool segment_intersection(P p1, P p2, P p3, P p4) { // end points are not allowed
return dcmp((p2 - p1) ^ (p3 - p1)) * dcmp((p2 - p1) ^ (p4 - p1)) < 0
&& dcmp((p4 - p3) ^ (p1 - p3)) * dcmp((p4 - p3) ^ (p2 - p3)) < 0;
}
bool parallel(Line l1, Line l2) {return same(l1.a * l2.b, l1.b * l2.a);}
P line_intersection(Line l1, Line l2) {
return P(-l1.b * l2.c + l1.c * l2.b, l1.a * l2.c - l1.c * l2.a) / (-l1.a * l2.b + l1.b * l2.a); }
double Area(vector<P> &p) {
double res = 0;
for(int i = 1; i < (int)p.size() - 1; i++)
res += (p[i] - p[0]) ^ (p[i + 1] - p[0]);
return res * 0.5;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
vector<P> vec;
for(int i = 0; i < n; i++) {
P p; cin >> p.x >> p.y;
vec.push_back(p);
}
bool flag = 1;
for(int i = 0; i < n; i++) {
P p0 = vec[i], p1 = vec[(i + 1) % n], p2 = vec[(i + 2) % n];
Vector v1 = p1 - p0, v2 = p2 - p1;
if(dcmp(v1 ^ v2) == -1) flag = 0;
}
cout << flag << '\n';
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#ifndef NULL
#define NULL 0
#endif
using namespace std;
const int MAXN = 50000 + 10;
const double pi = acos(-1.0);
const double inf = 1e100;
const double eps = 1e-12;
int zhengfu(double d)
{
if (fabs(d) < eps)
return 0;
if (d > 0)
return 1;
return -1;
}
int bijiao(double x, double y)
{
if (fabs(x - y) < eps)
return 0;
if (x > y)
return 1;
return -1;
}
struct vec {
double x, y;
double p;
vec(double x, double y)
{
this->x = x;
this->y = y;
}
vec() {}
vec operator*(const vec& i_T) const
{
return vec(x * i_T.x, y * i_T.y);
}
vec operator*(double u) const
{
return vec(x * u, y * u);
}
bool operator==(const vec& i_T) const
{
return x == i_T.x && y == i_T.y;
}
vec operator/(double u) const
{
return vec(x / u, y / u);
}
vec operator+(const vec& i_T)
{
return vec(x + i_T.x, y + i_T.y);
}
vec operator-(const vec& i_T)
{
return vec(x - i_T.x, y - i_T.y);
}
friend bool operator<(vec a, vec b)
{
return a.p < b.p;
}
} p[MAXN];
double chaji(vec A, vec B)
{
return A.x * B.y - A.y * B.x;
}
double xuanzhuan(vec a, vec b, vec c)
{
return chaji(b - a, c - a);
}
vec q[MAXN],tp;
int n;
void Graham(int& tail)
{
int zz = 0;
for (int i = 0; i < n; i++)
if (p[i].y < p[zz].y || (p[i].y == p[zz].y && p[zz].x > p[i].x))
zz = i;
swap(p[0], p[zz]);
for (int i = 1; i < n; i++) {
p[i] = p[i] - p[0];
p[i].p = atan2(p[i].y, p[i].x);
}
p[0].x = p[0].y = 0;
sort(p + 1, p + n);
q[0] = p[0];
tail = 0;
for (int i = 1; i < n; i++) {
while (tail>0 && xuanzhuan(q[tail - 1], q[tail], p[i]) < 0)
tail--;
q[++tail] = p[i];
}
}
int main()
{
//freopen("txt.txt", "w", stdout);
cin >> n;
for (int i = 0; i < n; i++)
cin >> p[i].x >> p[i].y;
int t = p[0].y, f = 1;
for (int i = 0; i < n; i++)
if (t != p[i].y) {
f = 0;
break;
}
if (!f) {
int tail;
Graham(tail);
cout << (tail+1 == n) << endl;
/*for (int i = 0; i <= tail; i++)
cout << q[i].x+tp.x << ' ' << tp.y+q[i].y << endl;*/
} else
cout << 1 << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
const double eps=1e-9;
struct dot{
double x,y;
double len(){return sqrt(x*x+y*y);}
dot(double a=0.0,double b=0.0){x=a,y=b;}
dot operator +(const dot&b)const {return dot(x+b.x,y+b.y);}
dot operator -(const dot&b)const {return dot(x-b.x,y-b.y);}
dot operator *(const double&z)const {return dot(x*z,y*z);}
bool operator <(const dot&b)const {return (x<b.x)||(x==b.x&&y<b.y);}
void read(){scanf("%lf%lf",&x,&y);}
};
struct line{
dot x1,x2;
void read(){x1.read();x2.read();};
dot getdot(){return x2-x1;}
};
double chaji(dot x,dot y){
return x.x*y.y-x.y*y.x;
}
double neiji(dot x,dot y){
return x.x*y.x+x.y*y.y;
}
double cos(dot x,dot y){
if (!x.len()||!y.len())return 0;
return neiji(x,y)/x.len()/y.len();
}
dot Projection(dot x,line y){//求x关于直线y的投影点(垂足)
y.x2=y.x2-y.x1;
x=x-y.x1;
double len=cos(x,y.x2)*x.len()/y.x2.len();
y.x2=y.x2*len;
return y.x2+y.x1;
}
dot Reflection(dot x,line y){//求x关于直线y的对称点
dot z=Projection(x,y);
return z*2-x;
}
int Counter_Clockwise(dot x,line y){//求两个向量的五种关系
x=x-y.x1;y.x2=y.x2-y.x1;
if (chaji(y.x2,x)!=0){
if (chaji(y.x2,x)>0)return 1;
return 2;
}
if (fabs(cos(x,y.x2)+1)<eps)return 3;
if (x.len()>y.x2.len())return 4;
return 5;
}
int Parallel_Orthogonal(line x,line y){//求两个直线是垂直还是平行还是其他
dot a=x.getdot(),b=y.getdot();
if (chaji(a,b)==0)return 2;
if (neiji(a,b)==0)return 1;
return 0;
}
int Intersection(line x,line y){//求两条直线是否相交
int Minx1=min(x.x1.x,x.x2.x),Maxx1=max(x.x1.x,x.x2.x);
int Minx2=min(y.x1.x,y.x2.x),Maxx2=max(y.x1.x,y.x2.x);
int Miny1=min(x.x1.y,x.x2.y),Maxy1=max(x.x1.y,x.x2.y);
int Miny2=min(y.x1.y,y.x2.y),Maxy2=max(y.x1.y,y.x2.y);
if (Minx1>Maxx2||Minx2>Maxx1)return 0;
if (Miny1>Maxy2||Miny2>Maxy1)return 0;
if (chaji(x.getdot(),y.x1-x.x1)*chaji(x.getdot(),y.x2-x.x1)>0)return 0;
if (chaji(y.getdot(),x.x1-y.x1)*chaji(y.getdot(),x.x2-y.x1)>0)return 0;
return 1;
}
dot CrossPoint(line x,line y){//求两条线段的交点
if (fabs(cos(x.getdot(),y.getdot())-1)<eps){
if (x.x2<y.x2)return y.x2;
else return x.x2;
}
else {
if (x.x2.x-x.x1.x==0){
double k2=(y.x2.y-y.x1.y)/(y.x2.x-y.x1.x),b2=y.x2.y-y.x2.x*k2;
return dot(x.x2.x,x.x2.x*k2+b2);
}
if (y.x2.x-y.x1.x==0){
double k1=(x.x2.y-x.x1.y)/(x.x2.x-x.x1.x),b1=x.x2.y-x.x2.x*k1;
return dot(y.x2.x,y.x2.x*k1+b1);
}
double k1=(x.x2.y-x.x1.y)/(x.x2.x-x.x1.x),b1=x.x2.y-x.x2.x*k1;
double k2=(y.x2.y-y.x1.y)/(y.x2.x-y.x1.x),b2=y.x2.y-y.x2.x*k2;
dot ans;
ans.x=(b2-b1)/(k1-k2);ans.y=ans.x*k1+b1;
return ans;
}
}
double Dis(dot x,dot y){//点到点的距离
return sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y));
}
double dist(line x,dot y){//点到线段的距离
dot P=Projection(y,x);
double k=neiji(x.getdot(),P-x.x1)/neiji(x.getdot(),x.getdot());
if (k<=0)return Dis(x.x1,y);
if (k>=1)return Dis(x.x2,y);
return Dis(y,P);
}
double Distance(line x,line y){//线段之间距离
return min(min(dist(x,y.x1),dist(x,y.x2)),min(dist(y,x.x1),dist(y,x.x2)));
}
struct polygon{
vector<dot > A;
double S(){//多边形面积
double ans=0;
for (int i=0;i+1<A.size();i++)ans+=chaji(A[i],A[i+1])/2;
if (A.size()>1)ans+=chaji(A[A.size()-1],A[0])/2;
return ans;
}
double C(){//多边形周长
double ans=0;
for (int i=0;i+1<A.size();i++)ans+=Dis(A[i],A[i+1]);
if (A.size()>1)ans+=Dis(A[A.size()-1],A[0]);
return ans;
}
void read(){
int n;scanf("%d",&n);A.resize(n);
for (int i=0;i<n;i++)A[i].read();
}
int Is_Convex(){
for (int i=0;i<A.size();i++)
if (chaji(A[(i+1)%A.size()]-A[i],A[(i+2)%A.size()]-A[(i+1)%A.size()])<0)
return 0;
return 1;
}
};
int main(){
polygon A;
A.read();
printf("%d\n",A.Is_Convex());
}
|
#include<bits/stdc++.h>
using namespace std;
#define ld long double
#define eps 1e-9
bool cmp(ld A , ld B){return A + eps > B && A - eps < B;}
class vec{
public:
ld x , y;
vec(ld _x = 0 , ld _y = 0) : x(_x) , y(_y){}
friend vec operator +(vec A , vec B){return vec(A.x + B.x , A.y + B.y);}
friend vec operator -(vec A , vec B){return vec(A.x - B.x , A.y - B.y);}
friend ld operator *(vec A , vec B){return A.x * B.x + A.y * B.y;}
friend ld operator %(vec A , vec B){return A.x * B.y - A.y * B.x;}
friend vec operator *(vec A , ld B){return vec(A.x * B , A.y * B);}
ld len(){return sqrt(x * x + y * y);}
ld len2(){return x * x + y * y;}
ld angle(){return atan2(y , x);}
};
vec getvec(){ld x , y; cin >> x >> y; return vec(x , y);}
class segment{
public:
vec st , ed , dir;
segment(vec _a = vec() , vec _b = vec()) : st(_a) , ed(_b) , dir(_b - _a){}
friend bool havesect(segment A , segment B){
ld P = A.st.x , Q = A.ed.x , X = B.st.x , Y = B.ed.x;
if(Q < P) swap(P , Q);
if(Y < X) swap(X , Y);
if(X - eps > Q || P - eps > Y) return 0;
P = A.st.y , Q = A.ed.y , X = B.st.y , Y = B.ed.y;
if(Q < P) swap(P , Q);
if(Y < X) swap(X , Y);
if(X - eps > Q || P - eps > Y) return 0;
return
((B.st - A.st) % A.dir) * (A.dir % (B.ed - A.st)) > -eps &&
((B.st - A.ed) % A.dir) * (A.dir % (B.ed - A.ed)) > -eps &&
((A.st - B.st) % B.dir) * (B.dir % (A.ed - B.st)) > -eps &&
((A.st - B.ed) % B.dir) * (B.dir % (A.ed - B.ed)) > -eps;
}
friend vec getsect(segment A , segment B){
ld t = ((A.st - B.st) % B.dir) / (B.dir % A.dir);
return A.st + (A.dir * t);
}
ld len(){return dir.len();}
ld len2(){return dir.len2();}
};
segment getseg(){vec A = getvec() , B = getvec(); return segment(A , B);}
ld dist(vec P , segment seg){//minimum dist from a point to a segment
ld T = ((P - seg.st) * seg.dir) / seg.len2();
if(T < 0) return (seg.st - P).len();
else if(T > 1) return (seg.ed - P).len();
else return (seg.st + (seg.dir * T) - P).len();
}
class polygon{//counter-clockwise
public:
vector < vec > point; int sz;
void input(){
cin >> sz; for(int i = 1 ; i <= sz ; ++i) point.push_back(getvec());
for(int i = 0 ; i < sz ; ++i) point.push_back(point[i]);
}
ld area(){
ld sum = 0;
for(int i = 0 ; i < sz ; ++i) sum += point[i] % point[i + 1];
return sum / 2;
}
bool isconvex(){
for(int i = 1 ; i <= sz ; ++i)
if((point[i - 1] - point[i]) % (point[i + 1] - point[i]) > eps) return 0;
return 1;
}
};
class convex : public polygon{
public:
bool checkin(vec P){
return 0;
}
};
int main(){
polygon T; T.input(); cout << fixed << setprecision(1) << T.isconvex() << endl;
return 0;
}
|
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
using namespace std;
#define MON 1000000007
#define INF (1<<29)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;
#define max(x, y) ((x)>(y)?(x):(y))
#define min(x, y) ((x)<(y)?(x):(y))
class Vec{
public:
double x, y;
Vec(double x = 0, double y = 0):x(x),y(y){}
Vec &read(){
cin >> x >> y;
return *this;
}
void print(){
printf("%.10lf %.10lf\n", x, y);
}
Vec operator+(const Vec &other)
{
Vec result = *this;
result.x += other.x;
result.y += other.y;
return result;
}
Vec operator-(const Vec &other)
{
Vec result = *this;
result.x -= other.x;
result.y -= other.y;
return result;
}
Vec operator*(const double &k)
{
Vec result = *this;
result.x *= k;
result.y *= k;
return result;
}
Vec operator/(const double &k)
{
Vec result = *this;
result.x /= k;
result.y /= k;
return result;
}
double cross(const Vec &other)
{
return x*other.y - y*other.x;
}
double dot(const Vec &other){
return x*other.x + y*other.y;
}
bool operator==(const Vec &other) const
{
return abs(x - other.x) < EPS && abs(y - other.y) < EPS;
}
double norm()
{
return sqrt(x*x+y*y);
}
double norm2()
{
return x*x+y*y;
}
Vec standard(){
Vec result = *this;
return result/result.norm();
}
};
//ccw:1, cw:-1, other:0
Int CCW(Vec a, Vec b, Vec c){
b = b - a;
c = c - a;
if(b.cross(c) > EPS)return -1;
if(b.cross(c) < -EPS)return 1;
return 0;
}
double dist(Vec a, Vec b){
return (a-b).norm();
}
class Line{
public:
Vec a, b;
Vec vect;
Line(Vec a = Vec(), Vec b = Vec()):a(a),b(b),vect(b-a){}
//projection
Vec proj(Vec p){
p = p - a;
return a + vect * vect.dot(p) / vect.norm2();
}
//reflection
Vec reflect(Vec p){
return proj(p) * 2 - p;
}
bool onSegment(Vec p){
return abs((p-a).cross(b-a)) < EPS && (p-a).dot(p-b) < EPS;
}
//other -> LineSegment(not inclusive), this -> Line
bool _intersect(Line &other){
return CCW(a, b, other.a) * CCW(a, b, other.b) < 0;
}
//other, this: both are LineSegment(inclusive)
bool intersect(Line &other){
return onSegment(other.a) || onSegment(other.b) || other.onSegment(a) || other.onSegment(b) || _intersect(other) && other._intersect(*this);
}
//low accuracy
Vec crossPoint(Line &other){
double ratio = (a - other.a).cross(vect) / other.vect.cross(vect);
return other.a + other.vect * ratio;
}
double dist(Vec p){
Vec pp = proj(p);
if(onSegment(pp))return ::dist(p, pp);
else return min(::dist(p, a), ::dist(p, b));
}
double dist(Line &other){
if(intersect(other))return 0;
return min(min(dist(other.a), dist(other.b)), min(other.dist(a), other.dist(b)));
}
};
//Signed Area
double area(vector<Vec> &v){
Int n = v.size();
double area = 0;
for(int i = 0;i < n;i++){
area += v[i].cross(v[(i+1)%n]);
}
return area / 2;
}
void ok(){
cout << "1" << endl;
exit(0);
}
void ng(){
cout << "0" << endl;
exit(0);
}
int main(){
Int n;
cin >> n;
vector<Vec> v;
Vec p;
for(int i = 0;i < n;i++){
v.push_back(Vec().read());
}
Int ccw = 0;
for(int i = 0;i < n;i++){
Vec a = v[i];
Vec b = v[(i+1)%n];
Vec c = v[(i+2)%n];
if(ccw == 0)ccw = CCW(a,b,c);
else{
if(CCW(a,b,c) == 0)continue;
if(ccw != CCW(a,b,c))ng();
}
}
ok();
return 0;
}
|
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <utility>
#define EPS 1e-10
using namespace std;
typedef long long ll;
struct Point{
double x,y;
Point(double x = 0.0,double y = 0.0): x(x),y(y) {}
Point operator + (Point p){
return Point(x + p.x,y + p.y);
}
Point operator - (Point p){
return Point(x - p.x,y - p.y);
}
Point operator * (double lambda){
return Point(x * lambda,y * lambda);
}
Point operator / (double lambda){
return Point(x / lambda,y / lambda);
}
double norm(){
return x * x + y * y;
}
double abs_(){
return sqrt(norm());
}
bool operator == (const Point &p)const
{
return abs(x - p.x) < EPS && abs(y - p.y) < EPS;
}
bool operator < (const Point &p)const
{
if(abs(x - p.x) < EPS) return y - p.y < -EPS;
else return x - p.x < EPS;
}
};
typedef Point Vector;
struct Segment{
Point p1,p2;
Segment(Point p1 = Point(),Point p2 = Point()): p1(p1),p2(p2) {}
};
typedef Segment Line;
struct Circle{
Point c;
double r;
Circle(Point c = Point(),double r = 0.0): c(c),r(r) {}
};
typedef vector<Point> Polygon;
double dot(Vector a,Vector b){
return a.x * b.x + a.y * b.y;
}
double det(Vector a,Vector b){
return a.x * b.y - b.x * a.y;
}
bool IsConvex(Polygon p){
int n = p.size();
for(int i=0;i<n;i++){
Point p1 = p[(i - 1 + n) % n],p2 = p[(i + 1) % n];
if(det(p2 - p[i],p1 - p[i]) < -EPS) return false;
}
return true;
}
Polygon p;
int n;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
Point p1;
scanf("%lf%lf",&p1.x,&p1.y);
p.push_back(p1);
}
printf("%d\n",(int)IsConvex(p));
return 0;
}
|
// Written By NewbieChd
#include <iostream>
#include <iomanip>
#include <cmath>
// #define double long double
using namespace std;
const int maxN = 100003;
const double eps = 1e-8, inf = 1e18;
inline double absolute(double x) { return x >= 0 ? x : -x; }
inline double square(double x) { return x * x; }
struct Vector {
double x, y;
Vector() {}
Vector(double x, double y) : x(x), y(y) {}
friend Vector operator+(const Vector& a, const Vector& b) { return Vector(a.x + b.x, a.y + b.y); }
friend Vector operator-(const Vector& a, const Vector& b) { return Vector(a.x - b.x, a.y - b.y); }
friend Vector operator*(const Vector& a, const double& b) { return Vector(a.x * b, a.y * b); }
friend Vector operator/(const Vector& a, const double& b) { return Vector(a.x / b, a.y / b); }
inline double length() { return sqrt(square(x) + square(y)); }
inline void read() { cin >> x >> y; }
inline void write() { cout << fixed << setprecision(10) << x << ' ' << y << '\n'; }
};
inline double distance(Vector a, Vector b) { return sqrt(square(b.x - a.x) + square(b.y - a.y)); }
inline double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; }
inline double cross(Vector a, Vector b) { return a.x * b.y - a.y * b.x; }
inline Vector project(Vector a, Vector b, Vector c) {
b = a - b;
return b * (dot(b, c - a) / (square(b.x) + square(b.y))) + a;
}
inline Vector reflect(Vector a, Vector b, Vector c) {
a = project(a, b, c);
return a * 2 - c;
}
inline bool intersect(Vector a, Vector b, Vector c, Vector d) {
if (max(min(a.x, b.x), min(c.x, d.x)) > min(max(a.x, b.x), max(c.x, d.x)) + eps ||
max(min(a.y, b.y), min(c.y, d.y)) > min(max(a.y, b.y), max(c.y, d.y)) + eps ||
cross(a - c, d - c) * cross(d - c, b - c) + eps < 0 ||
cross(c - a, b - a) * cross(b - a, d - a) + eps < 0)
return 0;
return 1;
}
struct Line {
double A, B, C;
Line() {}
Line(double A, double B, double C) : A(A), B(B), C(C) {}
};
inline Line getLine(Vector a, Vector b) { return Line(b.y - a.y, a.x - b.x, a.y * (b.x - a.x) - a.x * (b.y - a.y)); }
inline Vector crossPoint(Line a, Line b) {
double tmp = a.A * b.B - a.B * b.A;
return Vector((a.B * b.C - b.B * a.C) / tmp, (b.A * a.C - a.A * b.C) / tmp);
}
inline double PointSegDistance(Vector a, Vector b, Vector c) {
Vector l = b - a;
double tmp;
if ((tmp = dot(c - a, l) / (square(l.x) + square(l.y))) < eps)
return distance(c, a);
if (tmp + eps > 1)
return distance(c, b);
return distance(c, project(a, b, c));
}
inline double SegSegDistance(Vector a, Vector b, Vector c, Vector d) {
if (intersect(a, b, c, d))
return 0;
return min(min(PointSegDistance(a, b, c), PointSegDistance(a, b, d)),
min(PointSegDistance(c, d, a), PointSegDistance(c, d, b)));
}
Vector a[maxN];
inline double area(Vector* a, int n) {
double o = cross(a[n - 1], a[0]);
for (int i = 1; i < n; ++i)
o += cross(a[i - 1], a[i]);
return o / 2;
}
int main() {
ios::sync_with_stdio(false);
int n, i;
cin >> n;
for (i = 1; i <= n; ++i)
a[i].read();
if (cross(a[n] - a[n - 1], a[1] - a[n]) + eps < 0 ||
cross(a[1] - a[n], a[2] - a[1]) + eps < 0) {
cout << 0 << '\n';
return 0;
}
for (i = 3; i <= n; ++i)
if (cross(a[i - 1] - a[i - 2], a[i] - a[i - 1]) + eps < 0) {
cout << 0 << '\n';
return 0;
}
cout << 1 << '\n';
return 0;
}
|
#include<cmath>
#include<algorithm>
#include<iostream>
#include<vector>
#include<climits>
#include<cfloat>
#include<cstdio>
#define curr(P,i) P[(i)%P.size()]
#define next(P,i) P[(i+1)%P.size()]
#define prev(P,i) P[(i+P.size()-1)%P.size()]
using namespace std;
typedef double Real;
Real EPS = 1e-8;
const Real PI = acos(-1);
int sgn(Real a, Real b=0){return a<b-EPS?-1:a>b+EPS?1:0;}
Real sqr(Real a){return sqrt(max(a,(Real)0));}
struct Point{
Real add(Real a, Real b){
if(abs(a+b) < EPS*(abs(a)+abs(b)))return 0;
return a+b;
}
Real x, y;
Point(){}
Point(Real x,Real y) : x(x) , y(y){}
Point operator + (Point p){return Point(add(x,p.x), add(y,p.y));}
Point operator - (Point p){return Point(add(x,-p.x), add(y,-p.y));}
Point operator * (Real d){return Point(x*d,y*d);}
Point operator / (Real d){return Point(x/d,y/d);}
bool operator == (Point p){return !sgn(dist(p));}
bool operator < (Point p){return (p.x!=x)?p.x<x:p.y<y;}
Real norm(){return sqr(x*x+y*y);}
Real dist(Point a){return (*this-a).norm();}
Real dot(Point a){return x*a.x+y*a.y;}
Real cross(Point a){return x*a.y-y*a.x;}
//点pを中心に角度r(radius)だけ半時計回りに回転する
Point rotate(Real r,Point p = Point(0,0)){
Real ta=cos(r)*(x-p.x)-sin(r)*(y-p.y)+p.x;
Real tb=sin(r)*(x-p.x)+cos(r)*(y-p.y)+p.y;
return Point(ta,tb);
}
Real arg(){
if(sgn(x)>0)return atan(y/x);
if(sgn(x)<0)return atan(y/x)+PI;
if(sgn(y)>0)return PI/2;
if(sgn(y)<0)return 3*PI/2;
return 0;
}
};
//a -> b -> c
int ccw(Point a, Point b, Point c) {
b = b-a; c = c-a;
if (b.cross(c) > 0) return +1; // counter clockwise
if (b.cross(c) < 0) return -1; // clockwise
if (b.dot(c) < 0) return +2; // c--a--b on line
if (b.norm() < c.norm()) return -2; // a--b--c on line
return 0; // a--c--b on line
}
struct Polygon{
vector<Point>v;
Polygon(){}
Polygon(int n){v.resize(n);}
bool isConvex() {
for (int i=0; i<v.size();i++){
if (ccw(prev(v,i),curr(v,i),next(v,i))==-1)return false;
}
return true;
}
};
int main(void){
int n;
cin >> n;
Polygon p(n);
for(int i=0;i<n;i++)
cin >> p.v[i].x >> p.v[i].y;
cout << p.isConvex() << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
const int limN = 105;
typedef pair<int,int> pii;
#define x first
#define y second
int area(pii a, pii b, pii c) {
return a.x*b.y + b.x*c.y + c.x*a.y -
(a.y*b.x + b.y*c.x + c.y*a.x);
}
int sgno(int x) {
if(x==0)
return x;
return x<0? -1 : 1;
}
int N ;
pii ptos[limN];
bool funca(int proh) {
for(int i=0; i<N; i++) {
if(sgno(area(ptos[(i+N-1)%N], ptos[i], ptos[(i+1)%N])) == proh)
return false;
}
return true;
}
int main() {
scanf("%d", &N);
for(int i=0; i<N; i++)
scanf("%d%d", &ptos[i].x, &ptos[i].y);
printf("%d\n", (funca(1) || funca(-1))? 1 : 0);
}
|
#include<cstdio>
#define MAX 100
struct Point{double x, y;};
Point T[MAX];
double cross(double x1, double y1, double x2, double y2){
return x1*y2 - x2*y1;
}
double ccw(Point p1, Point p2, Point p3){
return cross(p2.x-p1.x, p2.y-p1.y, p3.x-p2.x, p3.y-p2.y);
}
int main(int argc, char const *argv[])
{
int n;
scanf("%d", &n);
for(int i =0; i<n; i++) scanf("%lf %lf", &T[i].x, &T[i].y);
double result;
bool flag = true;
for(int i=0; i<n; i++){
if(i==0){
result = ccw(T[n-1], T[i], T[i+1]);
}
else if(i == n-1){
result = ccw(T[i-1], T[i], T[0]);
}else{
result = ccw(T[i-1], T[i], T[i+1]);
}
if(result <0){
flag = false;
break;
}
}
if(flag) printf("1\n");
else printf("0\n");
return 0;
}
|
#include <vector>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <climits>
using namespace std;
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)
//???
class Point {
public:
double x, y;
Point (double x = 0, double y = 0):x(x), y(y){}
Point operator + (Point p){return Point(x + p.x, y + p.y);}
Point operator - (Point p){return Point(x - p.x, y - p.y);}
Point operator * (double a){return Point(a * x, a * y);}
Point operator / (double a){return Point(x / a, y / a);}
double norm(){return x*x + y*y;};
double absolute(){return sqrt(norm());};
bool operator < (const Point &p) const{
return x != p.x ? x < p.x : y < p.y;
}
bool operator == (const Point &p) const{
return equals(x, p.x) && equals(y, p.y);
}
};
typedef Point Vector; //????????????
//????????????a,b?????????
double cross(Vector a, Vector b){
return a.x * b.y - a.y * b.x;
}
int main(){
int n;
Vector a, b;
bool convex_flag = true;
cin >> n;
Vector *q = new Vector[n];
for (int i = 0; i < n; i++) {
cin >> q[i].x;
cin >> q[i].y;
}
for (int i = 0; i < n; i++) {
a = q[(i+1)%n] - q[i];
b = q[(i+2)%n] - q[i];
if (cross(a, b) < 0) {
convex_flag = false;
break;
}
}
if(convex_flag) printf("1\n");
else printf("0\n");
return 0;
} |
#include <bits/stdc++.h>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-9;
const ll mod=1e9+7;
const int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
typedef complex<double> C;
typedef pair<C,C> pp;
typedef vector<C> VP;
typedef vector<pp> VPP;
#define eq(a,b) (fabs(a-b)<eps)
#define veq(a,b) (eq(a.real(),b.real())&&eq(a.imag(),b.imag()))
const C O{0,0};
void In(C& p){
double x,y;
cin>>x>>y;
p=C(x,y);
}
void Out(C p){
cout<<(int)p.real()<<' '<<(int)p.imag()<<endl;
}
double Dot(C p,C q){
return p.real()*q.real()+p.imag()*q.imag();
}
double Det(C p,C q){
return p.real()*q.imag()-q.real()*p.imag();
}
bool is_Convex(VP p){
p.push_back(p[0]);
p.push_back(p[1]);
int n=p.size();
for(int i=1;i<n-1;i++) if(Det(p[i]-p[i-1],p[i+1]-p[i-1])<0) return 0;
return 1;
}
int n;
VP a;
int main(){
cin>>n;
a=VP(n);
for(auto &i:a) In(i);
cout<<is_Convex(a)<<endl;
}
|
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>
static const int MOD = 1000000007;
using ll = long long;
using u32 = uint32_t;
using namespace std;
template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;
using real = double;
real EPS = 1e-10;
struct Point {
real x, y;
Point& operator+=(const Point a) { x += a.x; y += a.y; return *this; }
Point& operator-=(const Point a) { x -= a.x; y -= a.y; return *this; }
Point& operator*=(const real k) { x *= k; y *= k; return *this; }
Point& operator/=(const real k) { x /= k; y /= k; return *this; }
Point operator+(const Point a) const {return Point(*this) += a; }
Point operator-(const Point a) const {return Point(*this) -= a; }
Point operator*(const real k) const {return Point(*this) *= k; }
Point operator/(const real k) const {return Point(*this) /= k; }
bool operator<(const Point &a) const { return (x != a.x ? x < a.x : y < a.y); }
explicit Point(real a = 0, real b = 0) : x(a), y(b) {};
};
istream& operator>> (istream& s, Point& P){
s >> P.x >> P.y;
return s;
}
inline real dot(Point a, Point b){ return a.x*b.x + a.y*b.y; }
inline real cross(Point a, Point b){ return a.x*b.y - a.y*b.x; }
inline real abs(Point a){ return sqrt(dot(a, a)); }
static constexpr int COUNTER_CLOCKWISE = 1;
static constexpr int CLOCKWISE = -1;
static constexpr int ONLINE_BACK = 2;
static constexpr int ONLINE_FRONT = -2;
static constexpr int ON_SEGMENT = 0;
int ccw(Point a, Point b, Point c){
b -= a; c -= a;
if(cross(b, c) > EPS) return COUNTER_CLOCKWISE;
if(cross(b, c) < -EPS) return CLOCKWISE;
if(dot(b, c) < 0) return ONLINE_BACK;
if(abs(b) < abs(c)) return ONLINE_FRONT;
return ON_SEGMENT;
}
struct Segment {
Point a, b;
Segment(Point x, Point y) : a(x), b(y) {};
};
bool intersect(Segment s, Segment t){
return (ccw(s.a, s.b, t.a)*ccw(s.a, s.b, t.b) <= 0 &&
ccw(t.a, t.b, s.a)*ccw(t.a, t.b, s.b) <= 0);
}
double distance(Segment s, Point c){
if(dot(s.b-s.a, c-s.a) < EPS) return abs(c-s.a);
if(dot(s.a-s.b, c-s.b) < EPS) return abs(c-s.b);
return abs(cross(s.b-s.a, c-s.a)) / abs(s.a-s.b);
}
double distance(Segment s, Segment t){
if(intersect(s, t)) return 0.0;
return min({distance(s, t.a), distance(s, t.b),
distance(t, s.a), distance(t, s.b)});
}
Point crossPoint(Segment s, Segment t){
real d1 = abs(cross(s.b-s.a, t.b-t.a));
real d2 = abs(cross(s.b-s.a, s.b-t.a));
if(d1 < EPS && d2 < EPS) return t.a;
return t.a+(t.b-t.a)*d2/d1;
}
Point project(Segment s, Point p){
Point Q = s.b-s.a;
return s.a + Q*(dot(p-s.a, Q) / dot(Q, Q));
}
Point refrect(Segment s, Point p){
Point Q = project(s, p);
return Q*2-p;
}
bool isOrthogonal(Segment s, Segment t){
return fabs(dot(s.b-s.a, t.b-t.a)) < EPS;
}
bool isparallel(Segment s, Segment t){
return fabs(cross(s.b-s.a, t.b-t.a)) < EPS;
}
using polygon = vector<Point>;
real area(polygon &v){
if(v.size() < 3) return 0.0;
real ans = 0.0;
for (int i = 0; i+1 < v.size(); ++i) {
ans += cross(v[i], v[i+1]);
}
ans += cross(v.back(), v.front());
return ans/2;
}
polygon convex_hull(polygon v){
int n = v.size();
sort(v.begin(),v.end());
int k = 0;
polygon ret(n*2);
for (int i = 0; i < n; ++i) {
while(k > 1 && cross(ret[k-1]-ret[k-2], v[k]-ret[k-1]) < 0) k--;
ret[k++] = v[i];
}
for(int i = n-2, t=k; i >= 0; i--){
while(k > t && cross(ret[k-1]-ret[k-2], v[i]-ret[k-1]) < 0) k--;
ret[k++] = v[i];
}
ret.resize(k-1);
return ret;
}
bool isconvex(polygon &P){
int n = P.size();
for (int i = 0; i < n; ++i) {
if(ccw(P[(i+n-1)%n], P[i], P[(i+1)%n]) == CLOCKWISE) return false;
}
return true;
}
int main() {
int n;
cin >> n;
polygon P(n);
for(auto &&i : P) cin >> i;
cout << isconvex(P) << "\n";
return 0;
}
|
#include <cstdio>
#include <utility>
#include <vector>
#include <complex>
#include <cmath>
using namespace std;
static const double EPS=1e-12;
static const double INF=1e24;
using Point=complex<double>;
using Plane=vector<Point>;
using Polygon=vector<Point>;
bool operator<(const Point &a, const Point &b) {
return real(a)!=real(b)? real(a)<real(b) : imag(a)<imag(b);
}
double cross_prod(const Point &a, const Point &b) {
return imag(conj(a)*b);
}
double dot_prod(const Point &a, const Point &b) {
return real(conj(a)*b);
}
enum {
ONLINE_FRONT=-2,
CLOCKWISE,
ON_SEGMENT,
COUNTER_CLOCKWISE,
ONLINE_BACK,
};
int ccwise(Point a, Point b, Point c) {
b -= a; c -= a;
if (cross_prod(b, c) > 0) {
return COUNTER_CLOCKWISE;
} else if (cross_prod(b, c) < 0) {
return CLOCKWISE;
} else if (dot_prod(b, c) < 0) {
return ONLINE_BACK;
} else if (norm(b) < norm(c)) {
return ONLINE_FRONT;
} else {
return ON_SEGMENT;
}
}
bool is_convex(Polygon g) {
size_t V=g.size();
for (size_t i=0; i<V; ++i) {
int state=ccwise(g[i], g[(i+1)%V], g[(i+2)%V]);
if (state == CLOCKWISE)
return false;
}
return true;
}
int main() {
size_t n;
scanf("%zu", &n);
Polygon g(n);
for (size_t i=0; i<n; ++i) {
double x, y;
scanf("%lf %lf", &x, &y);
g[i] = Point(x, y);
}
printf("%d\n", is_convex(g));
return 0;
} |
//
// main.cpp
// Computational_Geometry
//
// Created by ?????£??? on 2017/8/2.
// Copyright ?? 2017??´ ?????£???. All rights reserved.
//
#include <iostream>
#include <cstdio>
#include <cmath>
#include <memory>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
const double eps = 1e-8;
const double PI = acos(-1.0);
const int MAX=1e1;
int sgn(double x)
{
if (fabs(x)<eps) return 0;
if (x<0) return -1;
else return 1;
}
struct Point
{
double x,y;
Point(){}
Point(double _x,double _y){
x=_x;y=_y;
}
Point operator -(const Point &b)const{
return Point(x-b.x,y-b.y);
}
double operator ^(const Point &b)const{
return x*b.y - y*b.x;
}
double operator *(const Point &b)const{
return x*b.x - y*b.y;
}
void transXY(double B){
double tx=x,ty=y;
x=tx*cos(B) - ty*sin(B);
y=tx*sin(B) + ty*cos(B);
}
};
double CalcArea(Point p[],int n)
{
double res=0;
for (int i=0; i<n; i++) {
res+=(p[i]^p[(i+1)%n])/2;
}
return fabs(res);
}
bool isconvex(Point poly[],int n)
{
bool s[3];
memset(s, false, sizeof(s));
for (int i=0; i<n; i++) {
s[sgn((poly[(i+1)%n]-poly[i])^(poly[(i+2)%n]-poly[i]))+1]=true;
if (s[0] && s[2]) return false;
}
return true;
}
Point p[MAX];
int main(void) {
//freopen("/Users/mac/Desktop/C++?¨????/Computational_Geometry/Computational_Geometry/test.txt", "r", stdin);
int n;
scanf("%d\n",&n);
for (int i=0; i<n; i++) {
scanf("%lf%lf\n",&p[i].x,&p[i].y);
}
printf("%d\n",isconvex(p, n)?1:0);
return 0;
} |
#include<iostream>
#include<vector>
#include<iomanip>
#include<cmath>
#include<algorithm>
#include<cassert>
using namespace std;
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)
struct Point;
typedef Point Vector;
typedef vector<Point> Polygon;
struct Circle;
struct Segment;
typedef Segment Line;
double norm(Point a);
double abs(Point a);
double dot(Vector a, Vector b);
double cross(Vector a, Vector b);
double getDistance(Point a, Point b);
double getDistanceLP(Line l, Point p);
double getDistanceSP(Segment s, Point p);
double getDistance(Segment s1, Segment s2);
bool isOrthogonal(Vector a, Vector b);
bool isOrthogonal(Point a1, Point a2, Point b1, Point b2);
bool isOrthogonal(Segment s1, Segment s2);
bool isParallel(Vector a, Vector b);
bool isParallel(Point a1, Point a2, Point b1, Point b2);
bool isParallel(Segment s1, Segment s2);
int ccw(Point p0, Point p1, Point p2);
bool intersect(Point p1, Point p2, Point p3, Point p4);
bool intersect(Segment s1, Segment s2);
bool intersect(Circle c, Line l); // ちゃんと検証はしてない
Point project(Segment s, Point p);
Point reflect(Segment s, Point p);
Point getCrossPoint(Segment s1, Segment s2);
pair<Point,Point> getCrossPoints(Circle c, Line l);
double area(Polygon g); // convexでなくてもよい. absを取れば符号付き面積
bool isConvex(Polygon g); // O(n^2) 線形時間アルゴリズムが存在するらしい
struct Point{
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {}
Point operator + (Point p){ return Point(x+p.x, y+p.y); }
Point operator - (Point p){ return Point(x-p.x, y-p.y); }
Point operator * (double a){ return Point(a*x, a*y); }
Point operator / (double a){ return Point(x/a, y/a); }
double abs() { return sqrt(norm()); }
double norm() { return x*x + y*y; }
bool operator < (const Point &p) const{
return x != p.x ? x < p.x : y < p.y;
}
bool operator == (const Point &p) const{
return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;
}
};
typedef Point Vector;
typedef vector<Point> Polygon;
struct Circle{
Point c;
double r;
Circle(Point c = Point(), double r = 0.0) : c(c), r(r) {}
};
struct Segment{
Point p1, p2;
Segment(Point p1, Point p2) : p1(p1), p2(p2) {}
};
typedef Segment Line;
double norm(Point a){
return a.x * a.x + a.y * a.y;
}
double abs(Point a){
return sqrt(norm(a));
}
double dot(Vector a, Vector b){
return a.x * b.x + a.y * b.y;
}
double cross(Vector a, Vector b){
return a.x * b.y - a.y * b.x;
}
double getDistance(Point a, Point b){
return abs(a - b);
}
double getDistanceLP(Line l, Point p){
return abs(cross(l.p2 - l.p1, p - l.p1) / abs(l.p2 - l.p1));
}
double getDistanceSP(Segment s, Point p){
if(dot(s.p2-s.p1, p-s.p1) < 0.0) return abs(p-s.p1);
if(dot(s.p1-s.p2, p-s.p2) < 0.0) return abs(p-s.p2);
return getDistanceLP(s, p);
}
double getDistance(Segment s1, Segment s2){
if(intersect(s1, s2)) return 0.0;
return min({getDistanceSP(s1, s2.p1), getDistanceSP(s1, s2.p2),
getDistanceSP(s2, s1.p1), getDistanceSP(s2, s1.p2)});
}
bool isOrthogonal(Vector a, Vector b){
return equals(dot(a, b), 0.0);
}
bool isOrthogonal(Point a1, Point a2, Point b1, Point b2){
return isOrthogonal(a1-a2, b1-b2);
}
bool isOrthogonal(Segment s1, Segment s2){
return equals(dot(s1.p2-s1.p1, s2.p2-s2.p1), 0.0);
}
bool isParallel(Vector a, Vector b){
return equals(cross(a, b), 0.0);
}
bool isParallel(Point a1, Point a2, Point b1, Point b2){
return isParallel(a1-a2, b1-b2);
}
bool isParallel(Segment s1, Segment s2){
return equals(cross(s1.p2-s1.p1, s2.p2-s2.p1), 0.0);
}
static const int COUNTER_CLOCKWISE = 1;
static const int CLOCKWISE = -1;
static const int ONLINE_BACK = 2; // p2->p0->p1
static const int ONLINE_FRONT = -2; // p0->p1->p2
static const int ON_SEGMENT = 0; // p0->p2->p1
int ccw(Point p0, Point p1, Point p2){
Vector a = p1 - p0;
Vector b = p2 - p0;
if(cross(a, b) > EPS) return COUNTER_CLOCKWISE;
if(cross(a, b) < -EPS) return CLOCKWISE;
if(dot(a, b) < -EPS) return ONLINE_BACK;
if(norm(a) < norm(b)) return ONLINE_FRONT;
return ON_SEGMENT;
}
bool intersect(Point p1, Point p2, Point p3, Point p4){
return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 &&
ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0);
}
bool intersect(Segment s1, Segment s2){
return intersect(s1.p1, s1.p2, s2.p1, s2.p2);
}
bool intersect(Circle c, Line l){
return getDistanceLP(l, c.c) < c.r+EPS;
}
Point project(Segment s, Point p){
Vector base = s.p2 - s.p1;
double r = dot(p - s.p1, base) / norm(base);
return s.p1 + base * r;
}
Point reflect(Segment s, Point p){
return p + (project(s, p) - p) * 2.0;
}
Point getCrossPoint(Segment s1, Segment s2){
Vector base = s2.p2 - s2.p1;
double d1 = abs(cross(base, s1.p1-s2.p1));
double d2 = abs(cross(base, s1.p2-s2.p1));
double t = d1 / (d1 + d2);
return s1.p1 + (s1.p2 - s1.p1) * t;
}
pair<Point,Point> getCrossPoints(Circle c, Line l){
assert(intersect(c, l));
Vector pr = project(l, c.c);
Vector e = (l.p2 - l.p1) / abs(l.p2 - l.p1);
double base = sqrt(c.r * c.r - norm(pr - c.c));
return make_pair(pr + e*base, pr - e*base);
}
double area(Polygon g){
int n = g.size();
Point o(0.0, 0.0);
double res = 0.0;
for(int i = 0; i < n; i++) res += cross(g[i]-o, g[(i+1)%n]-o);
return abs(res) / 2.0;
}
bool isConvex(Polygon g){
bool ret = true;
int n = g.size();
for(int i = 0; i < n; i++){
for(int j = i+1; j < n; j++){
if(cross(g[i]-g[(i+n-1)%n], g[j]-g[(i+n-1)%n]) < -EPS || cross(g[(i+1)%n]-g[i], g[j]-g[i]) < -EPS){
ret = false;
}
}
}
return ret;
}
int main(){
int n;
cin >> n;
Polygon g;
for(int i = 0; i < n; i++){
double x, y;
cin >> x >> y;
g.push_back(Point(x,y));
}
cout << isConvex(g) << endl;
return 0;
}
|
#include <iostream>
#include <vector>
using namespace std;
template <class T> void readVal(vector<T>& vec, int idx) {
T input;
std::cin >> input;
vec[idx] = input;
}
int main()
{
int size;
cin >> size;
vector<int> x(size);
vector<int> y(size);
for (int i = 0; i < size; i++) {
readVal(x, i);
readVal(y, i);
}
int ans(1), dir(0), vecx(0), vecy(0), new_vecx(0), new_vecy(0);
for (int i = 0; i <= size; i++) {
new_vecx = x[(i + 1) % size] - x[i % size];
new_vecy = y[(i + 1) % size] - y[i % size];
int cross_prod = (vecx * new_vecy - vecy * new_vecx);
if (cross_prod != 0) {
if (cross_prod * dir >= 0) {
dir = cross_prod > 0 ? 1 : -1;
}
else {
ans = 0;
break;
}
}
vecx = new_vecx;
vecy = new_vecy;
}
cout << ans << "\n";
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for(int i = (a); i < (b); ++i)
#define per(i, a, b) for(int i = (b) - 1; i >= (a); --i)
#define sz(a) (int)a.size()
#define de(c) cout << #c << " = " << c << endl
#define dd(c) cout << #c << " = " << c << " "
#define all(a) a.begin(), a.end()
#define pw(x) (1ll<<(x))
#define endl "\n"
typedef long long ll;
typedef double db;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef db T;
const db eps = 1e-9 , pi = acosl(-1.);
int sgn(T x){return (x>eps)-(x<-eps);}
struct P{
T x,y; P(){} P(T x,T y):x(x),y(y){}
P operator - (const P&b) const {return P(x-b.x,y-b.y);}
P operator + (const P&b) const {return P(x+b.x,y+b.y);}
T operator * (const P&b) const {return x*b.x+y*b.y;}
T operator / (const P&b) const {return x*b.y-y*b.x;}
P operator * (const T&k) const {return P(x*k,y*k);}
P operator / (const T&k) const {return P(x/k,y/k);}
};
T norm(P a){return a*a;}
T abs(P a) {return sqrtl(norm(a));}
P proj(P p,P a,P b){return (b-a)*((p-a)*(b-a)/norm(b-a))+a;}
P reflect(P p,P a,P b){return proj(p,a,b)*2-p;}
T cross(P o,P a,P b){return (a-o)/(b-o);}
int crossOp(P o,P a,P b){return sgn(cross(o,a,b));}
bool onPS(P p,P s,P t){return sgn((t-s)/(p-s))==0&&sgn((p-s)*(p-t))<=0;}
struct L{ P s,t;L(){} L(P s,P t):s(s),t(t){}};
P insLL(L a,L b){ // line x line
P s = a.s - b.s , v = a.t - a.s , w = b.t - b.s;
db k1 = s / w , k2 = w / v;
if(sgn(k2) == 0) return abs(b.s - a.s) < abs(b.t - a.s) ? b.s : b.t;
return a.s + v * (k1 / k2);
}
bool isSS(L a,L b){ // seg x seg , replace x->y to accelerate
T c1=(a.t-a.s)/(b.s-a.s),c2=(a.t-a.s)/(b.t-a.s);
T c3=(b.t-b.s)/(a.s-b.s),c4=(b.t-b.s)/(a.t-b.s);
return sgn(c1) * sgn(c2) <= 0 && sgn(c3) * sgn(c4) <= 0 &&
sgn(max(a.s.x,a.t.x) - min(b.s.x,b.t.x)) >= 0 &&
sgn(max(b.s.x,b.t.x) - min(a.s.x,a.t.x)) >= 0 &&
sgn(max(a.s.y,a.t.y) - min(b.s.y,b.t.y)) >= 0 &&
sgn(max(b.s.y,b.t.y) - min(a.s.y,a.t.y)) >= 0;
}
db disPL(P p,L a){return fabs((a.t-a.s)/(p-a.s)) / abs(a.t-a.s);}
db disPS(P p,L a){ // p x seg dis
if(sgn((a.t-a.s)*(p-a.s)) == -1) return abs(p-a.s);
if(sgn((a.s-a.t)*(p-a.t)) == -1) return abs(p-a.t);
return disPL(p,a);
}
db disSS(L a,L b){ // seg x seg dis
if(isSS(a,b)) return 0;
return min(min(disPS(a.s,b),disPS(a.t,b)),min(disPS(b.s,a),disPS(b.t,a)));
}
typedef vector<P> polygon;
T area(polygon A) { // multiple 2 with integer type
T res=0;
rep(i,0,sz(A)) res+=A[i]/(A[(i+1)%sz(A)]);
return fabs(res) / 2;
}
bool isconvex(polygon A){ // counter-clockwise
bool ok=1;int n=sz(A);
rep(i,0,2) A.pb(A[i]);
rep(i,0,n) ok&=((A[i+1]-A[i])/(A[i+2]-A[i]))>=0;
return ok;
}
P a, b, c, d;
polygon poly;
int main() {
std::ios::sync_with_stdio(0);
std::cin.tie(0);
int q;
cin >> q;
cout << setiosflags(ios::fixed);
cout << setprecision(1);
while(q--) {
cin >> a.x >> a.y;
poly.pb(a);
}
cout << isconvex(poly) << endl;
return 0;
}
|
#include <iostream>
#include <complex>
#include <vector>
#include <algorithm>
#include <functional>
#include <iomanip>
using namespace std;
template<typename T> bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<typename T> bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
// 実数
using Real = double;
// 点
using Point = complex<Real>;
const Real EPS = 1e-10, PI = acos(-1);
// 実数同士の比較
inline bool eq(Real a, Real b) { return fabs(b - a) < EPS; }
Point operator*(const Point& p, const Real& d) {
return Point(real(p) * d, imag(p) * d);
}
// 点の入力
istream& operator>>(istream& is, Point& p) {
Real a, b;
is >> a >> b;
p = Point(a, b);
return is;
}
//// 点の出力
//ostream &operator<<(ostream &os, Point &p) {
// os << fixed << setprecision(10) << p.real() << " " << p.imag();
//}
// 点 p を反時計回りに theta 回転
Point rotate(Real theta, const Point& p) {
return Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());
}
// ラジアンを度数に変換
Real radian_to_degree(Real r) {
return (r * 180.0 / PI);
}
// 度数をラジアンに変換
Real degree_to_radian(Real d) {
return (d * PI / 180.0);
}
// a-b-c の角度のうち小さい方を返す
Real get_angle(const Point& a, const Point& b, const Point& c) {
const Point v(b - a), w(c - a);
Real alpha = atan2(v.imag(), v.real()), beta = atan2(w.imag(), w.real());
if (alpha > beta) swap(alpha, beta);
Real theta = (beta - alpha);
return min(theta, 2 * acos(-1) - theta);
}
// ソート x座標が小さい順に並べる x座標が同じならy座標が小さい順
namespace std {
bool operator<(const Point& a, const Point& b) {
return !eq(a.real(), b.real()) ? a.real() < b.real() : a.imag() < b.imag();
}
}
// 2点を通る直線
struct Line {
Point a, b;
Line() = default;
Line(Point a, Point b) : a(a), b(b) {}
Line(Real A, Real B, Real C) // Ax + By = C
{
if (eq(A, 0)) a = Point(0, C / B), b = Point(1, C / B);
else if (eq(B, 0)) b = Point(C / A, 0), b = Point(C / A, 1);
else a = Point(0, C / B), b = Point(C / A, 0);
}
friend ostream& operator<<(ostream& os, Line& p) {
return os << p.a << " to " << p.b;
}
friend istream& operator>>(istream& is, Line& a) {
return is >> a.a >> a.b;
}
};
// 2点を結ぶ線分
struct Segment : Line {
Segment() = default;
Segment(Point a, Point b) : Line(a, b) {}
};
// 円
struct Circle {
// 中心
Point p;
// 半径
Real r;
Circle() = default;
Circle(Point p, Real r) : p(p), r(r) {}
};
// 点集合
using Points = vector< Point >;
// ポリゴン 反時計回り
using Polygon = vector< Point >; // 注意!! 凸多角形は反時計回りに与える.(保証されない場合は面積が負なら reverse をかける)
// 線分集合
using Segments = vector< Segment >;
// 直線集合
using Lines = vector< Line >;
// 円集合
using Circles = vector< Circle >;
// 外積
Real cross(const Point& a, const Point& b) {
return real(a) * imag(b) - imag(a) * real(b);
}
// 内積
Real dot(const Point& a, const Point& b) {
return real(a) * real(b) + imag(a) * imag(b);
}
// 点の回転方向
// +1
//
// +2 a 0 b -2
//
// -1
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C&lang=jp
int ccw(const Point& a, Point b, Point c) {
b = b - a, c = c - a;
if (cross(b, c) > EPS) return +1; // "COUNTER_CLOCKWISE"
if (cross(b, c) < -EPS) return -1; // "CLOCKWISE"
if (dot(b, c) < 0) return +2; // "ONLINE_BACK"
if (norm(b) < norm(c)) return -2; // "ONLINE_FRONT"
return 0; // "ON_SEGMENT"
}
// 線分同士の交差判定
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B
bool intersect(const Segment& s, const Segment& t) {
return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;
}
Real CalcDist(const Segment& s, const Point& p) {
double t = dot(s.b - s.a, p - s.a) / norm(s.b - s.a);
Point c = s.a + (s.b - s.a) * t;
Real res = 1000000.0;
if (t > -EPS && t < 1.0 + EPS) return sqrt(norm(p - c));
chmin(res, sqrt(norm(p - s.b)));
chmin(res, sqrt(norm(p - s.a)));
return res;
}
int main() {
int n;
cin >> n;
Polygon polygon;
for (int i = 0; i < n; ++i) {
Point p;
cin >> p;
polygon.emplace_back(p);
}
bool is_convex = true;
for (int i = 0; i < n; ++i) {
if(ccw(polygon[i], polygon[(i + 1) % n], polygon[(i + 2) % n]) == -1) {
is_convex = false;
break;
}
}
cout << is_convex << endl;
//cout << fixed << setprecision(1) << ans << endl;
}
|
//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <climits>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <queue>
#include <random>
#include <cctype>
#include <complex>
#include <regex>
using namespace std;
#define C_MAX(a, b) ((a)>(b)?(a):(b))
#define SHOW_VECTOR(v) {std::cerr << #v << "\t:";for(const auto& xxx : v){std::cerr << xxx << " ";}std::cerr << "\n";}
#define SHOW_MAP(v) {std::cerr << #v << endl; for(const auto& xxx: v){std::cerr << xxx.first << " " << xxx.second << "\n";}}
#define EPS 1e-8
#define EQ(a, b) (abs((a)-(b)) < EPS)
const double PI = acos(-1.0);
inline int signum(double x) { return (abs(x) < EPS) ? 0 : (x > 0) ? 1 : -1; }
typedef complex<double> P;
typedef vector<P> Poly;
double dot(P a, P b) {
return a.real() * b.real() + a.imag() * b.imag();
}
double cross(P a, P b) {
return a.real() * b.imag() - a.imag() * b.real();
}
double toRadian(double degree) {
return degree * PI / 180.0;
}
double toDegree(double radian) {
return radian * 180.0 / PI;
}
int ccw(P a, P b, P c) {
b -= a;
c -= a;
int sign = signum(cross(b, c));
if (sign != 0) return sign;
if (signum(dot(b, c)) == -1) return 2;
if (abs(b) < abs(c)) return -2;
return 0;
}
double dis_p_li(P a, P b, P c) {
b -= a;
c -= a;
return abs(cross(b, c)) / abs(b);
}
double dis_p_li_ss(P a, P b, P c) {
P x1 = b - a;
P y1 = c - a;
P x2 = a - b;
P y2 = c - b;
if (signum(dot(x1, y1)) < 0) return abs(y1);
if (signum(dot(x2, y2)) < 0) return abs(y2);
return dis_p_li(a, b, c);
}
bool is_intersected_li_ss(P a1, P a2, P b1, P b2) {
return (ccw(a1, a2, b1) * ccw(a1, a2, b2) <= 0) &&
(ccw(b1, b2, a1) * ccw(b1, b2, a2) <= 0);
}
double dis_li_s_li_s(P a1, P a2, P b1, P b2) {
if (is_intersected_li_ss(a1, a2, b1, b2)) return 0.0;
return min({
dis_p_li_ss(a1, a2, b1),
dis_p_li_ss(a1, a2, b2),
dis_p_li_ss(b1, b2, a1),
dis_p_li_ss(b1, b2, a2)
});
}
double area_tri(double a, double b, double c) {
double s = (a + b + c) / 2;
return sqrt(s * (s - a) * (s - b) * (s - c));
}
double area_tri(P a, P b, P c) {
double x = abs(b - a);
double y = abs(c - b);
double z = abs(a - c);
return area_tri(x, y, z);
}
double area_poly(Poly poly) {
if (poly.size() < 3) return 0.0;
double ans = 0.0;
for (int i = 0; i < poly.size(); i++) ans += cross(poly[i], poly[(i + 1) % poly.size()]);
return abs(ans) / 2.0;
}
bool is_convex_poly(Poly poly) {
if (poly.size() < 3)return false;
int s = -3;
int n = poly.size();
for (int i = 0; i < poly.size(); i++) {
int r = ccw(poly[i % n], poly[(i + 1) % n], poly[(i + 2) % n]);
if (abs(r) == 1 && s == -3) s = r;
if (s * r == -1) return false;
}
return true;
}
int main() {
int N;
cin >> N;
Poly poly(N);
for (int i = 0; i < N; i++) {
double x, y;
cin >> x >> y;
poly[i] = P(x, y);
}
bool result = is_convex_poly(poly);
if (result) cout << 1 << endl;
else cout << 0 << endl;
}
|
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
using namespace std;
using T = double;
const T EPS = 1e-10;
T torad(int deg) {return (T)(deg) * M_PI / 180;}
T todeg(T ang) {return ang * 180 / M_PI;}
/* Point */
using P = complex<T>;
#define x real()
#define y imag()
template <typename T> inline bool eq(T p, T q) { return abs(p - q) < EPS; }
inline int sgn(T x) { return (T(0) < x) - (x < T(0)); }
inline T dot(P v, P w) { return (conj(v) * w).x; }
inline T cross(P v, P w) { return (conj(v) * w).y; }
inline bool isOrth(P v, P w) { return dot(v, w) == 0; }
inline bool isPara(P v, P w) { return cross(v, w) == 0; }
inline P translate(P p, P v) { return p + v; }
inline P scale(P p, P c, T k) { return c + (p - c) * k; }
inline P rot(P p, T a) { return p * polar(1.0, a); }
inline P rot90(P p) { return {-p.y, p.x}; }
inline T orient(P a, P b, P c) { return cross(b - a, c - a); }
istream& operator>>(istream& is, P& p) { T xx, yy; is >> xx >> yy; p = P(xx, yy); return is; }
bool cmpX(const P& a, const P& b) { return a.x != b.x ? a.x < b.x : a.y < b.y; }
namespace std {
bool operator < (const P& a, const P& b) { return cmpX(a, b); }
}
/* Polygon */
vector<P> makeStandard(vector<P> p) {
int n = p.size(), j = -1;
for (int i = 0; i < n; ++i) {
if (j == -1 || p[i].y < p[j].y) j = i;
}
vector<P> res;
if (p[(j - 1 + n) % n].x <= p[j].x && p[j].x <= p[(j + 1) % n].x) {
for (int i = 0; i < n; ++i) {
res.push_back(p[(i + j) % n]);
}
} else {
for (int i = 0; i < n; ++i) {
res.push_back(p[(i - j + n) % n]);
}
}
return res;
}
bool isConvex(vector<P> p) {
p = makeStandard(p); // p[0] has minimum y and p is counterclockwise
for (int i = 0, n = p.size(); i < n; ++i) {
P a = p[i], b = p[(i + 1) % n], c = p[(i + 2) % n];
if (orient(a, b, c) < 0) return false;
if (b.y != p[0].y && b.y < min(a.y, c.y)) return false;
if (b.y != p[0].y && a.y == b.y && b.y == c.y && a.x <= b.x && b.x <= c.x) return false;
}
return true;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<P> ps(n);
REP (i, n) cin >> ps[i];
cout << isConvex(ps) << endl;
}
|
#include<cmath>
#include<algorithm>
#include<iostream>
#include<vector>
#include<climits>
#include<cfloat>
#include<cstdio>
#define curr(P, i) P[(i) % P.size()]
#define next(P, i) P[(i+1) % P.size()]
#define prev(P, i) P[(i+P.size()-1) % P.size()]
using namespace std;
long double EPS = 1e-8;
const double PI = acos(-1);
long double add(long double a,long double b){
if(abs(a+b) < EPS * (abs(a)+abs(b)))return 0;
return a+b;
}
struct point{
long double x, y;
point(){}
point(long double x,long double y) : x(x) , y(y){}
point operator + (point p){
return point(add(x,p.x), add(y,p.y));
}
point operator - (point p){
return point(add(x,-p.x), add(y,-p.y));
}
point operator * (double d){
return point(x*d,y*d);
}
};
typedef point Vector;
long double dot(point a, point b) {
return (a.x * b.x + a.y * b.y);
}
long double cross(point a, point b) {
return (a.x * b.y - a.y * b.x);
}
long double norm(point a){
return sqrt(a.x*a.x+a.y*a.y);
}
static const int COUNTER_CLOCKWISE = 1;
static const int CLOCKWISE = -1;
static const int ONLINE_BACK = 2;
static const int ONLINE_FRONT = -2;
static const int ON_SEGMENT = 0;
int ccw( point p0, point p1, point p2 ){
Vector a=p1-p0;
Vector b=p2-p0;
if (cross(a,b)>EPS) return COUNTER_CLOCKWISE;
if (cross(a,b)<-EPS) return CLOCKWISE;
if (dot(a, b)<-EPS) return ONLINE_BACK;
if (norm(a)<norm(b)) return ONLINE_FRONT;
return ON_SEGMENT;
}
bool is_convex(vector<point> pol){
for(int i=0;i<pol.size();i++){
if(ccw(prev(pol,i),curr(pol,i),next(pol,i))==-1)return false;
}
return true;
}
int main(void){
int n;
cin >> n;
vector<point>pol(n);
for(int i=0;i<n;i++)cin >> pol[i].x >> pol[i].y;
cout << is_convex(pol) << endl;
return 0;
} |
#include <iostream>
using namespace std;
int main()
{
int n, d;
int x[102], y[102];
bool s = 1;
cin >> n;
for (int i = 0; i < n; i++)
cin >> x[i] >> y[i];
x[n] = x[0];
y[n] = y[0];
x[n + 1] = x[1];
y[n + 1] = y[1];
for (int i = 0; i < n; i++) {
d = (x[i + 1] - x[i]) * (y[i + 2] - y[i]) - (x[i + 2] - x[i]) * (y[i + 1] - y[i]);
if (d < 0) {
s = 0;
break;
}
}
cout << s << endl;
return 0;
}
|
#include <iostream>
#include <complex>
#include <cmath>
#include <iomanip>
#include <vector>
#include <algorithm>
using namespace std;
typedef complex<double> xy;
double eps = 1e-9;
double dot_product(xy a,xy b) {return (conj(a)*b).real();}
double cross_product(xy a,xy b) {return (conj(a)*b).imag();}
double dist_lp(xy a1,xy a2,xy p){
if(dot_product(a2-a1,p-a1)<eps) return abs(p-a1);
if(dot_product(a1-a2,p-a2)<eps) return abs(p-a2);
return abs(cross_product(a2-a1,p-a1))/abs(a2-a1);
}
xy projection(xy p,xy b) {return b*dot_product(p,b)/norm(b);}
xy projection2(xy p1,xy p2,xy p){
p -= p1; p2 -= p1;
xy proj = projection(p,p2);
return p1+proj;
}
bool is_online(xy a1,xy a2,xy p){
return abs(a1-p)+abs(a2-p)<=abs(a1-a2)+eps;
}
bool is_intersected(xy a1, xy a2, xy b1, xy b2){
if(is_online(a1,a2,b1) || is_online(a1,a2,b2)) return true;
if(is_online(b1,b2,a1) || is_online(b1,b2,a2)) return true;
return (cross_product(a2-a1,b1-a1)*cross_product(a2-a1,b2-a1)<-eps) &&
(cross_product(b2-b1,a1-b1)*cross_product(b2-b1,a2-b1))<-eps;
}
double dist_ll(xy a1,xy a2,xy b1,xy b2){
if(is_intersected(a1,a2,b1,b2)) return 0;
return min({dist_lp(a1,a2,b1),dist_lp(a1,a2,b2),dist_lp(b1,b2,a1),dist_lp(b1,b2,a1)
,abs(a1-b1),abs(a1-b2),abs(a2-b1),abs(a2-b2)});
}
double area_of_polygon(vector<xy>& v){
int n = v.size();
double res = 0;
for(int i=0;i+2<n;i++){
res += cross_product(v[i+1]-v[0],v[i+2]-v[0]);
}
return res/2;
}
bool is_convex(vector<xy>& v){
int n = v.size();
for(int i=0;i<n;i++){
xy a = v[i%n],b = v[(i+1)%n],c = v[(i+2)%n];
if(cross_product(b-a,c-a)<-eps) return false;
}
return true;
}
int N;
vector<xy> v;
int main(){
cin >> N;
double x,y;
for(int i=0;i<N;i++){
cin >> x >> y;
v.push_back(xy(x,y));
}
cout << (is_convex(v)? 1:0) << endl;
}
|
#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
typedef double D;
const D EPS = 1e-8;
const D INF = 1e10;
const D PI = M_PI;
struct P {
D x, y;
P(D xs, D ys) : x(xs), y(ys) { }
P() { }
};
P operator +(P a, P b) { return P(a.x + b.x, a.y + b.y); }
P operator -(P a, P b) { return P(a.x - b.x, a.y - b.y); }
P operator *(P p, D s) { return P(p.x * s, p.y * s); }
D inp(P a, P b) { return a.x*b.x + a.y*b.y; }
D outp(P a, P b) { return a.x*b.y - a.y*b.x; }
D norm(P p) { return inp(p, p); }
D abs(P p) { return sqrt(norm(p)); }
bool eq(P a, P b) { return abs(a - b) < EPS; }
D arg(P p) { return atan2(p.y, p.x); }
P rot90(P p) {
return P(-p.y, p.x);
}
P rot(P p, D radian) {
P q;
q.x = cos(radian)*p.x - sin(radian)*p.y;
q.y = sin(radian)*p.x + cos(radian)*p.y;
return q;
}
struct L : vector<P> {
P a, b;
L(P as, P bs) : a(as), b(bs) { }
};
P projection(L l, P p) { // ??卒???l???????????????p?????????
P a = l.b - l.a;
P b = p - l.a;
D t = inp(b, a) / norm(a);
return l.a + a * t;
}
P reflection(L l, P p) { // ??卒???l???????????????p???????属?
return p + (projection(l, p) - p) * 2;
}
int ccw(P a, P b, P c) {
b = b - a; c = c - a;
// a - b - c ????????????????????即???
if(outp(b, c) > EPS) return +1; // ???????即???????
if(outp(b, c) < -EPS) return -1; // ????即???????
// a - b - c ?????卒??????????????其??即???
if(inp(b, c) < 0) return +2; // c - a - b
if(norm(b) < norm(c)) return -2; // a - b - c
return 0; // a - c - b
}
bool iSS(L s, L t) { // ????????????
return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&
ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;
}
bool iSP(L s, P p) {
return ccw(s.a, s.b, p) == 0;
}
D dSP(L s, P p) {
P r = projection(s, p);
if(iSP(s, r)) return abs(r - p);
return min(abs(s.a - p), abs(s.b - p));
}
D dSS(L s, L t) {
if(iSS(s, t)) return 0;
return min(min(dSP(s, t.a), dSP(s, t.b)),
min(dSP(t, s.a), dSP(t, s.b)));
}
/* ?????????????????即??????iSS(s,t)????蔵???????????????即 */
P cLL(L l, L m) {
D d = outp(m.b - m.a, l.b - l.a);
return l.a + (l.b - l.a) * outp(m.b - m.a, m.b - l.a) * (1.0 / d);
}
typedef vector<P> Poly;
P at(const Poly &ps, int i) { return ps[i % ps.size()]; }
D polyArea(const Poly &ps) {
D res = 0.0;
for(int i = 0; i < (int)ps.size(); i++) {
res += outp(at(ps, i), at(ps, i + 1));
}
return res / 2.0;
}
struct C {
P p; D r;
C(P ps, D rs) : p(ps), r(rs) { }
C() { }
};
int main() {
int N; scanf(" %d", &N);
Poly ps(N);
for(int i = 0; i < N; i++) {
scanf(" %lf %lf", &ps[i].x, &ps[i].y);
}
int k = 0;
int f = 1;
for(int i = 0; i < N; i++) {
int ks = ccw(at(ps, i), at(ps, i + 1), at(ps, i + 2));
if(ks == 1 || ks == -1) {
if(k == 0) k = ks;
else if(k != ks) f = 0;
}
}
cout << f << endl;
} |
#include <iostream>
#include <cmath>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
#define EPS 1e-10
#define equal(a,b) (fabs(a-b) < EPS)
#define PI acos(-1)
struct Point{
double x,y;
Point(){}
Point(double x,double y) : x(x),y(y) {}
Point operator + (const Point &p)const{
return Point(x + p.x , y + p.y);
}
Point operator - (const Point &p)const{
return Point(x - p.x , y - p.y);
}
Point operator * (const double &k)const{
return Point(x * k , y * k);
}
Point operator / (const double &k)const{
return Point(x / k , y / k);
}
double dot(const Point &p)const{
return x*p.x + y*p.y;
}
double cross(const Point &p)const{
return x*p.y - p.x*y;
}
double dist(const Point &p)const{
return sqrt(pow(x-p.x,2) + pow(y-p.y,2));
}
double norm(){
return x*x + y*y;
}
double Abs(){
return sqrt(norm());
}
};
#define COUNTER_CLOCKWISE 1
#define CLOCKWISE -1
#define ONLINE_BACK 2
#define ONLINE_FRONT -2
#define ON_SEGMENT 0
typedef Point Vector;
int ccw(Point p0,Point p1,Point p2){
Vector a = p1 - p0;
Vector b = p2 - p0;
if(a.cross(b) > EPS){ return COUNTER_CLOCKWISE; }
if(a.cross(b) < -EPS){ return CLOCKWISE; }
if(a.dot(b) < -EPS){ return ONLINE_BACK; }
if(a.norm() < b.norm()){ return ONLINE_FRONT; }
return ON_SEGMENT;
}
typedef vector<Point> Polygon;
#define prev(G,i) (G[(i-1+G.size())%G.size()])
#define curr(G,i) (G[(i)%G.size()])
#define next(G,i) (G[(i+1)%G.size()])
bool isConvex(const Polygon &p){
for(int i = 0 ; i < (int)p.size() ; i++){
if(ccw(prev(p,i),curr(p,i),next(p,i)) == CLOCKWISE){
return false;
}
}
return true;
}
int main(){
int N;
cin >> N;
Polygon p(N);
for(int i = 0 ; i < N ; i++){
cin >> p[i].x >> p[i].y;
}
cout << isConvex(p) << endl;
return 0;
} |
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
typedef long long LL;
const double Pi = acos(-1.0);
const int INf = 0x7fffffff;
const double eps = 1e-8;
int sgn(double d) {
if(fabs(d) < eps)
return 0;
if(d > 0)
return 1;
return -1;
}
int dcmp(double x, double y) {
if(fabs(x - y) < eps)
return 0;
if(x > y)
return 1;
return -1;
}
struct Point {
double x, y;
Point(double _x = 0, double _y = 0):x(_x), y(_y){}
};
typedef Point Vector;
Vector operator + (Vector A, Vector B) {
return Vector(A.x + B.x, A.y + B.y);
}
Vector operator - (Point A, Point B) {
return Vector(A.x - B.x, A.y - B.y);
}
Vector operator * (Vector A, double p) {
return Vector(A.x * p, A.y * p);
}
Vector operator / (Vector A, double p) {
return Vector(A.x / p, A.y / p);
}
bool operator == (const Point &a, const Point &b) {
if(sgn(a.x-b.x) == 0 && sgn(a.y-b.y) == 0)
return true;
return false;
}
double Dot(Vector A, Vector B) {
return A.x * B.x + A.y * B.y;
}
double Cross(Vector A, Vector B) {
return A.x * B.y - B.x * A.y;
}
double Length(Vector A) {
return sqrt(Dot(A, A));
}
double Angle(Vector A, Vector B) {
return acos(Dot(A, B) / Length(A) / Length(B));
}
double Area2(Point A, Point B, Point C) {
return Cross(B - A, C - A);
}
Vector Rotate(Vector A, double rad) { // 逆时针旋转rad
return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) - A.y * cos(rad));
}
Vector Normal(Vector A) { //A左转90°的单位法向量
double L = Length(A);
return Vector(-A.y / L, A.x / L);
}
bool ToLeftTest(Point a, Point b, Point c) {
return Cross(b - a, c - a) > 0;
}
struct Line {
Point v, p;
Line(Point v, Point p) : v(v), p(p){}
Point point(double t) {
return v + (p - v) * t;
}
};
Point GetLineIntersection(Point P, Vector v, Point Q, Vector w) {
Vector u = P - Q;
double t = Cross(w, u) / Cross(v, w);
return P + v * t;
}
double DistanceToLine(Point P, Point A, Point B) {
Vector v1 = B - A, v2 = P - A;
return fabs(Cross(v1, v2) / Length(v1));
}
double DistanceToSegment(Point P, Point A, Point B) {
if(A == B)
return Length(P - A);
Vector v1 = B - A, v2 = P - A, v3 = P - B;
if(sgn(Dot(v1, v2)) < 0)
return Length(v2);
if(sgn(Dot(v1, v3)) > 0)
return Length(v3);
return DistanceToLine(P, A, B);
}
Point GetLineProjection(Point P, Point A, Point B) {
Vector v = B - A;
return A + v * (Dot(v, P - A) / Dot(v, v));
}
bool OnSegment(Point p, Point a1, Point a2){
return sgn(Cross(a1-p, a2-p)) == 0 && sgn(Dot(a1-p, a2-p)) < 0;
}
int PointAtSegment(Point p, Point a, Point b) {
Vector v1 = p - a, v2 = b - a;
if(sgn(Cross(v1, v2)) == -1)
return -1;
else if(sgn(Cross(v1, v2)) == 1)
return 1;
else if(sgn(Dot(v1, v2)) == -1)
return -1;
else if(sgn(Length(v1) - Length(v2)) == 1)
return 1;
return 0;
}
bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){
return PointAtSegment(b1, a1, a2) * PointAtSegment(b2, a1, a2) <= 0 && PointAtSegment(a1, b1, b2) * PointAtSegment(a2, b1, b2) <= 0;
}
double DistancePointToSegment(Point p, Point a, Point b) {
if(sgn(Dot(p - a, b - a)) == -1)
return Length(p - a);
else if(sgn(Dot(p - b, a - b)) == -1)
return Length(p - b);
return DistanceToLine(p, a, b);
}
double SegmentsDistance(Point a1, Point a2, Point b1, Point b2) {
if(SegmentProperIntersection(a1, a2, b1, b2))
return 0;
return min(min(DistancePointToSegment(a1, b1, b2), DistancePointToSegment(a2, b1, b2)), min(DistancePointToSegment(b1, a1, a2), DistancePointToSegment(b2, a1, a2)));
}
double PolygonArea(Point *p, int n) {
double s = 0.0;
for (int i = 1; i < n - 1; ++i)
s += Cross(p[i] - p[0], p[i + 1] - p[0]) / 2.0;
return s;
}
Point p[105];
bool GrahamCmp1(Point a, Point b) {
if(dcmp(a.y, b.y) == 0)
return dcmp(a.x, b.x) < 0;
return dcmp(a.y, b.y) < 0;
}
bool GrahamCmp2(Point a, Point b) {
int judge = sgn(Cross(b - a, p[0] - a));
if(judge > 0)
return 1;
else if (judge == 0 && Length(p[0] - a) < Length(p[0] - b))
return 1;
return 0;
}
Point Stack[105];
int Graham(Point *p, int n) {
sort(p, p + n, GrahamCmp1);
Stack[0] = p[0];
sort(p + 1, p + n, GrahamCmp2);
Stack[1] = p[1];
int top = 1;
for (int i = 2; i < n; ++i) {
while(sgn(Cross(Stack[top] - Stack[top-1], p[i] - Stack[top - 1])) <= 0)
--top;
Stack[++top] = p[i];
}
return top;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%lf%lf", &p[i].x, &p[i].y);
}
for (int i = 0; i < n - 1; ++i) {
if(sgn(Cross(p[i+1] - p[i], p[(i+2)%(n)] - p[i])) < 0) {
printf("0\n");
return 0;
}
}
printf("1\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using CP = complex<long double>;
#define X real()
#define Y imag()
const long double PI = acos(-1.0L);
const long double EPS = 1e-10;
// conj(x) : complex conjugate,(0,1)->(0,-1)
// abs(x) : dist between(0,0) and x
// norm(x) : abs(x) * abs(x)
// arg(x) : argment,[-PI,PI]
// dot(a,b) = |a||b|cos x
long double dot(CP a, CP b) { return (conj(a) * b).X; }
// cross(a,b) : area of parallelogram
// sign : a-> b ,counter clockwise? + : -
long double cross(CP a, CP b) { return (conj(a) * b).Y; }
long double corner(CP a, CP b) {
//[0,PI]
return acos(dot(a, b) / (abs(a) * abs(b)));
}
CP projection(CP p, CP s, CP t) {
CP base = t - s;
long double r = dot(p - s, base) / norm(base);
return s + base * r;
}
CP reflection(CP p, CP s, CP t) {
CP tmp = (projection(p, s, t) - p);
tmp *= 2;
return p + tmp;
}
CP intersectionLL(CP a, CP b, CP c, CP d) {
return a + (b - a) * (cross(d - c, c - a) /
cross(d - c, b - a));
}
bool on_seg(CP a, CP b, CP p) {
// if not use end point, dot(a - p, b - p) < 0
return abs(cross(a - p, b - p)) <= 1e-10 &&
dot(a - p, b - p) <= 0;
}
// crossing lines? (a,b) and (c,d)
bool iscross(CP a, CP b, CP c, CP d) {
// parallel
if(abs(cross(a - b, c - d)) <= 1e-10) {
return on_seg(a, b, c) || on_seg(a, b, d) ||
on_seg(c, d, a) || on_seg(c, d, b);
}
CP isp = intersectionLL(a, b, c, d);
return on_seg(a, b, isp) && on_seg(c, d, isp);
}
long double distLP(CP a, CP b, CP p) {
return abs(cross(b - a, p - a) / abs(b - a));
}
// segmentver.
long double distSP(CP a, CP b, CP p) {
if(dot(b - a, p - a) < 0) return abs(p - a);
if(dot(a - b, p - b) < 0) return abs(p - b);
return distLP(a, b, p);
}
// segment and segment
long double distSS(CP a, CP b, CP c, CP d) {
long double res = 1e18;
if(iscross(a, b, c, d)) return 0.0L;
res = min(res, distSP(a, b, c));
res = min(res, distSP(a, b, d));
res = min(res, distSP(c, d, a));
res = min(res, distSP(c, d, b));
return res;
}
// counter clockwise
bool is_convex(const vector<CP> &v) {
int n = v.size();
for(int i = 0; i < n; ++i)
if(cross(v[(i + 1) % n] - v[i],
v[(i + 2) % n] - v[(i + 1) % n]) < -EPS)
return 0;
return 1;
}
vector<CP> convex_hull(vector<CP> &ps) {
auto lmd = [&](const CP &l, const CP &r) {
if(l.X != r.X) return l.X < r.X;
return l.Y < r.Y;
};
vector<CP> qs;
int psize = ps.size();
sort(ps.begin(), ps.end(), lmd);
int k = 0;
qs.resize(psize * 2);
for(int i = 0; i < psize; ++i) {
while(k > 1 && cross(qs[k - 1] - qs[k - 2],
ps[i] - qs[k - 1]) <= 0)
--k;
qs[k++] = ps[i];
}
for(int i = psize - 2, t = k; i >= 0; --i) {
while(k > t && cross(qs[k - 1] - qs[k - 2],
ps[i] - qs[k - 1]) <= 0)
--k;
qs[k++] = ps[i];
}
qs.resize(k - 1);
return qs;
}
long double convex_diameter(vector<CP> &newv) {
vector<CP> v = convex_hull(newv);
int n = v.size(), i = 0, j = 0;
if(n == 2) return abs(v[0] - v[1]);
for(int k = 0; k < n; ++k) {
if(v[k].X < v[i].X) i = k;
if(v[k].X > v[j].X) j = k;
}
long double res = 0;
int si = i, sj = j;
while(i != sj || j != si) {
res = max(res, abs(v[i] - v[j]));
if(cross(v[(i + 1) % n] - v[i], v[(j + 1) % n] - v[j]) <
0)
(++i) %= n;
else
(++j) %= n;
}
return res;
}
struct Circle {
CP o;
long double r;
Circle(long double _x = 0.0L, long double _y = 0.0L,
long double _r = 0.0L)
: o(CP(_x, _y)), r(_r) {}
};
void intersectionCL(Circle ci, CP s, CP t, CP &res1,
CP &res2) {
res1 = res2 = projection(ci.o, s, t);
long double r = sqrtl(ci.r * ci.r - norm(res1 - ci.o));
t -= s;
t *= r / abs(t);
res1 += t;
res2 -= t;
if(res1.X > res2.X ||
(res1.X == res2.X && res1.Y > res2.Y))
swap(res1, res2);
}
long double polygonarea(vector<CP> &v) {
int n = v.size();
long double res = 0;
for(int i = 0; i < n; ++i)
res +=
(v[(i - 1 + n) % n].X - v[(i + 1) % n].X) * v[i].Y;
return res / 2.0L;
}
long long n, q;
vector<CP> v;
int main() {
cout << fixed << setprecision(10);
cin >> n;
for(int i = 0; i < n; ++i) {
long double a, b;
cin >> a >> b;
v.emplace_back(a, b);
}
cout << is_convex(v) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> using vec = vector<T>;
template<class T> using vvec = vector<vec<T>>;
using R = double;
using P = complex<R>;
#define x real()
#define y imag()
const R eps = 1e-9,PI = acos(-1);
bool equal(R a,R b){return abs(b-a)<eps;}
bool equal0(R a){return equal(a,0.0);}
P operator*(const P& p,const R& d){
return P(p.x*d,p.y*d);
}
istream &operator>>(istream& is,P& p){
R a,b;
is >> a >> b;
p = P(a,b);
return is;
}
ostream &operator<<(ostream& os,P& p){
os << fixed << setprecision(10) << p.real() << " " << p.imag();
}
//内積
double dot(P a,P b) {return (conj(a)*b).real();}
//外積
double cross(P a,P b) {return (conj(a)*b).imag();}
struct L{
P a,b;
L(P a,P b):a(a),b(b){}
L(R A,R B,R C){
if(equal(A,0)) a = P(0,C/B),b = P(1,C/B);
else if(equal(B,0)) b = P(C/A,0),b = P(C/A,1);
else a = P(0,C/B),b = P(C/A,0);
}
};
//pをbに射影
P projection(P p,P b) {return b*dot(p,b)/norm(b);}
//pとp1 to p2 に射影
P projection(L l,P p){
p -= l.a; l.b -= l.a;
P proj = projection(p,l.b);
return l.a+proj;
}
struct S:L{
S(P a,P b):L(a,b){}
};
//点の回転方向を判定
int ccw(P p0,P p1,P p2){
p1 -= p0,p2 -= p0;
if(cross(p1,p2)>eps) return 1; //"COUNTER_CLOCKWISE"
if(cross(p1,p2)<-eps) return -1; //"CLOCKWISE"
if(dot(p1,p2)<0) return 2; //"ONLINE_BACK"
if(norm(p1)<norm(p2)) return -2; //"ONLINE_FRONT"
return 0; //"ON_SEGMENT"
}
bool is_parallel(L l1,L l2){
return equal0(cross(l1.b-l1.a,l2.b-l2.a));
}
bool is_orthogonal(L l1,L l2){
return equal0(dot(l1.b-l1.a,l2.b-l2.a));
}
//pをlに関して対称移動
P reflection(L l,P p){
P pj = projection(l,p);
return 2.0*pj-p;
}
//pがl上にあるか
bool is_online(L l,P p){
return abs(ccw(l.a,l.b,p))!=1;
}
bool is_onsegment(S s,P p){
return ccw(s.a,s.b,p)==0;
}
//線分の交差判定
bool intersect(S s,S t){
return ccw(s.a,s.b,t.a)*ccw(s.a,s.b,t.b)<=0 && ccw(t.a,t.b,s.a)*ccw(t.a,t.b,s.b)<=0;
}
//2直線の交点
P crosspoint(L l1,L l2){
R a = cross(l1.b-l1.a,l2.b-l2.a);
R b = cross(l1.b-l1.a,l1.b-l2.a);
if(equal0(abs(a)) && equal0(abs(b))) return l2.a;
return l2.a+(l2.b-l2.a)*b/a;
}
P crosspoint(S s1, S s2){
return crosspoint(L(s1),L(s2));
}
R dist(P a,P b){
return abs(a-b);
}
R dist(L l,P p){
return abs(p-projection(l,p));
}
R dist(L l1,L l2){
return is_parallel(l1,l2)? 0:dist(l1,l2.a);
}
R dist(S s,P p){
P r = projection(s,p);
if(is_onsegment(s,r)) return abs(r-p);
return min(dist(s.a,p),dist(s.b,p));
}
R dist (S s1,S s2){
if(intersect(s1,s2)) return 0;
return min({dist(s1,s2.a),dist(s1,s2.b),
dist(s2,s1.a),dist(s2,s1.b)});
}
using Polygon = vec<P>;
R area_of_polygon(Polygon& v){
int n = v.size();
R res = 0;
for(int i=0;i+2<n;i++){
res += cross(v[i+1]-v[0],v[i+2]-v[0]);
}
return res/2;
}
bool is_convex(Polygon& v){
int n = v.size();
for(int i=0;i<n;i++){
P a = v[i%n],b = v[(i+1)%n],c = v[(i+2)%n];
if(cross(b-a,c-a)<-eps) return false;
}
return true;
}
int main(){
int N;
cin >> N;
Polygon pol(N);
for(int i=0;i<N;i++){
cin >> pol[i];
}
cout << is_convex(pol) << endl;
}
|
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
#include<string>
#include <math.h>
#include<algorithm>
#include<functional>
#define ll long long
#define inf 999999999
#define pa pair<int,int>
#define EPS (1e-10)
#define equals(a,b) (fabs((a)-(b))<EPS)
using namespace std;
class Point{
public:
double x,y;
Point(double x=0,double y=0):x(x),y(y) {}
Point operator + (Point p) {return Point(x+p.x,y+p.y);}
Point operator - (Point p) {return Point(x-p.x,y-p.y);}
Point operator * (double a) {return Point(x*a,y*a);}
Point operator / (double a) {return Point(x/a,y/a);}
double absv() {return sqrt(norm());}
double norm() {return x*x+y*y;}
bool operator < (const Point &p) const{
return x != p.x ? x<p.x: y<p.y;
}
bool operator == (const Point &p) const{
return fabs(x-p.x)<EPS && fabs(y-p.y)<EPS;
}
};
typedef Point Vector;
struct Segment{
Point p1,p2;
};
double dot(Vector a,Vector b){
return a.x*b.x+a.y*b.y;
}
double cross(Vector a,Vector b){
return a.x*b.y-a.y*b.x;
}
//----------------kokomade temple------------
int zz(double a){
if(a>EPS) return 1;
else if(a<-EPS) return -1;
else return 0;
}
int main(){
int n,k=0;
double x,y;
cin>>n;
Point a[110];
Vector v[110];
for(int i=0;i<n;i++){
cin>>x>>y;
a[i].x=x;
a[i].y=y;
}
a[n]=a[0];
for(int i=0;i<n;i++){
v[i]=a[i]-a[i+1];
}
v[n]=v[0];
int ans=0;
for(int i=0;i<n;i++){
ans=ans+zz(cross(v[i],v[i+1]));
if(zz(cross(v[i],v[i+1]))==0) k++;
}
if(ans==n-k || ans==-n+k) cout<<"1"<<endl;
else cout<<"0"<<endl;
} |
#include <iostream>
#include <algorithm>
#include <cassert>
#include <iomanip>
#include <vector>
#include <cmath>
using namespace std;
template<class T>
ostream& operator<<(ostream& os, const vector<T>& vs) {
if (vs.empty()) return os << "[]";
os << "[" << vs[0];
for (int i = 1; i < vs.size(); i++) os << " " << vs[i];
return os << "]";
}
const double EPS = 1e-7;
struct Point {
double x, y;
Point() {}
Point(double x, double y) : x(x), y(y) {}
Point operator+(const Point& p) const { return Point(x + p.x, y + p.y); }
Point operator-(const Point& p) const { return Point(x - p.x, y - p.y); }
Point operator*(double k) const { return Point(k * x, k * y); }
Point operator/(double k) const { return Point(x / k, y / k); }
};
double dot(const Point& a, const Point& b) { return a.x * b.x + a.y * b.y; }
double cross(const Point& a, const Point& b) { return a.x * b.y - a.y * b.x; }
double norm(const Point& a) { return sqrt(dot(a, a)); }
Point rot90(const Point& p) { return Point(p.y, -p.x); } // 時計回りに90度回転
ostream& operator<<(ostream& os, const Point& p) { return os << "(" << p.x << "," << p.y << ")"; }
istream& operator>>(istream& is, Point& p) { return is >> p.x >> p.y; }
int ccw(Point a, Point b, Point c){
b = b - a; c = c - a;
if (cross(b, c) > EPS) return +1; // a,b,cの順に反時計周り
if (cross(b, c) < -EPS) return -1; // a,b,cの順に時計周り
if (dot(b, c) < 0) return +2; // c--a--b 直線
if (norm(b) < norm(c)) return -2; // a--b--c 直線
return 0; // a--c--b 直線
}
// 頂点は反時計回りで与えられると仮定.
typedef vector<Point> Polygon;
bool convex(const Polygon& vs) {
int N = vs.size();
for (int i = 0; i < N; i++) {
Point a = vs[(i + N - 1) % N];
Point b = vs[i];
Point c = vs[(i + 1) % N];
if (ccw(a, b, c) == -1) return false;
}
return true;
}
int main() {
int N; cin >> N;
Polygon P(N);
for (int i = 0; i < N; i++) cin >> P[i];
cout << convex(P) << endl;
return 0;
} |
#include<cstdio>
#include<vector>
#include<algorithm>
#include<utility>
#include<numeric>
#include<iostream>
#include<array>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<list>
#include<functional>
#define _USE_MATH_DEFINES
#include<math.h>
#include<map>
#define INF 200000000
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
struct OwnVector2
{
double x, y;
OwnVector2(double x, double y) :x(x), y(y) {}
OwnVector2() :OwnVector2(0, 0) {}
OwnVector2 operator+(const OwnVector2& v)const
{
return OwnVector2(x + v.x, y + v.y);
}
OwnVector2 operator-(const OwnVector2& v)const
{
return OwnVector2(x - v.x, y - v.y);
}
OwnVector2 operator*(const double v)const
{
return OwnVector2(x*v, y*v);
}
bool operator==(const OwnVector2& v)const
{
return abs(x - v.x) <= 0.0000000000001&&abs(y - v.y) <= 0.00000000000001;
}
bool operator!=(const OwnVector2& v)const
{
return abs(x - v.x) > 0.0000000000001 || abs(y - v.y) > 0.00000000000001;
}
double dot(const OwnVector2& v)const
{
return x*v.x + y*v.y;
}
double cross(const OwnVector2& v)const
{
return x*v.y - y*v.x;
}
double length()const
{
return sqrt(x*x + y*y);
}
double length2()const
{
return x*x + y*y;
}
};
OwnVector2 vertices[100];
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
int q;
cin >> q;
for (int i = 0; i < q; i++)
{
double x, y;
cin >> x >> y;
vertices[i] = OwnVector2(x, y);
}
bool isConvex = true;
for (int i = 0; i < q; i++)
{
OwnVector2 front, back;
front = vertices[(i + 1) % q] - vertices[i];
back = vertices[(i + q - 1) % q] - vertices[i];
if (front.cross(back) < 0)
{
isConvex = false;
break;
}
}
printf(isConvex ? "1\n" : "0\n");
return 0;
}
|
#include <iostream>
#include <vector>
#include <cstdio>
#include <cmath>
using namespace std;
const double EPS = 1e-7;
const double INF = 1e12;
struct Vec {
double x,y;
Vec() {}
Vec(double x, double y) {
this->x = x, this->y = y;
}
void read() {
scanf("%lf %lf", &x, &y);
}
void prt() {
printf("%.9f %.9f\n", x, y);
}
double len() {
return sqrt(x * x + y * y);
}
double len2() {
return x * x + y * y;
}
Vec operator +(const Vec& o)const {
return Vec(x + o.x, y + o.y);
}
Vec operator -(const Vec& o)const {
return Vec(x - o.x, y - o.y);
}
Vec operator *(const double& k)const {
return Vec(k * x, k * y);
}
double operator *(const Vec& o)const {
return x * o.x + y * o.y;
}
double operator ^(const Vec& o)const {
return x * o.y - y * o.x;
}
Vec rotate(double ang){
return Vec(x * cos(ang) - y * sin(ang), x * sin(ang) + y * cos(ang));
}
Vec change(double l) {
if(len() < EPS) return *this;
return (*this) * (l/len());
}
};
struct Line {
Vec A1, A2;
Line() {}
Line(Vec A1, Vec A2) {
this->A1 = A1, this->A2 = A2;
}
double len() {
return (A2-A1).len();
}
double Len2() {
return (A2-A1).len2();
}
};
double disToLine(Vec P, Line L) {
return abs((P-L.A1)^(L.A2-L.A1)) / L.len();
}
Vec projection(Vec P, Line L) {
double shadowLen = (P - L.A1) * (L.A2 - L.A1);
return L.A1 + (L.A2-L.A1).change(shadowLen);
}
bool onseg(Vec P, Line L) {
if(disToLine(P, L) > EPS) return 0;
return (P-L.A1) * (P-L.A2) <= 0;
}
Vec Lineintersect(Line L1, Line L2) {
// 0 ~ (L2.A1 - L1.A1) ^ (L2.A2 - L1.A1);
// 1 ~ (L2.A1 - L1.A2) ^ (L2.A2 - L1.A2);
double F0 = (L2.A1 - L1.A1) ^ (L2.A2 - L1.A1);
double F1 = (L2.A1 - L1.A2) ^ (L2.A2 - L1.A2);
if (abs(F1 - F0) < EPS) return Vec(INF, INF);
return L1.A1 + (L1.A2 - L1.A1) * (- F0 / (F1 - F0));
}
Vec Segintersect(Line L1, Line L2) {
Vec P = Lineintersect(L1, L2);
if (P.x == INF) return P;
if (onseg(P, L1) && onseg(P, L2)) return P;
return Vec(INF, INF);
}
bool disToSeg(Vec P, Line L) {
double ans = min((P-L.A1).len(), (P-L.A2).len());
ans = min(ans, (P - projection(P,L)).len());
return ans;
}
struct Polygon {
int n;
vector<Vec> v;
void read() {
scanf("%d", &n);
v.resize(2*n);
for(int i=0;i<n;i++){
Vec tmp; tmp.read();
v[i] = v[i+n] = tmp;
}
}
double getArea() {
double ans=0;
for(int i=0;i<n;i++){
ans=ans+(v[i]^v[i+1]);
}
return abs(ans/2);
}
bool isConvex() {
for(int i=0;i<n;i++){
//printf("%.2f\n", (v[i+1]-v[i])^(v[i+2]-v[i+1]));
if( ((v[i+1]-v[i])^(v[i+2]-v[i+1])) < 0 )
return 0;
}
return 1;
}
} poly;
int main(){
poly.read();
printf("%d\n", poly.isConvex());
}
|
//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <climits>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <queue>
#include <random>
#include <cctype>
#include <complex>
#include <regex>
using namespace std;
#define C_MAX(a, b) ((a)>(b)?(a):(b))
#define SHOW_VECTOR(v) {std::cerr << #v << "\t:";for(const auto& xxx : v){std::cerr << xxx << " ";}std::cerr << "\n";}
#define SHOW_MAP(v){std::cerr << #v << endl; for(const auto& xxx: v){std::cerr << xxx.first << " " << xxx.second << "\n";}}
typedef complex<double> P;
typedef vector<P> Poly;
#define EPS 1e-8
#define EQ (abs((a)-(b))<EPS)
const double PI = acos(-1.0);
inline int signum(double x) { return abs(x) < EPS ? 0 : x > 0 ? 1 : -1; }
double dot(P a, P b) {
return a.real() * b.real() + a.imag() * b.imag();
}
double cross(P a, P b) {
return a.real() * b.imag() - a.imag() * b.real();
}
int ccw(P a, P b, P c) {
b -= a;
c -= a;
int sign = signum(cross(b, c));
if (abs(sign) == 1) return sign;
if (signum(dot(b, c)) < 0) return -2;
if (abs(b) < abs(c)) return 2;
return 0;
}
double area_poly(Poly poly) {
double ans = 0.0;
for (int i = 0; i < poly.size(); i++) ans += cross(poly[i], poly[(i + 1) % poly.size()]);
return abs(ans) / 2.0;
}
bool is_convex(Poly poly) {
if (poly.size() < 3) return false;
int s = -3;
int n = poly.size();
for (int i = 0; i < poly.size(); i++) {
int r = ccw(poly[i], poly[(i + 1) % n], poly[(i + 2) % n]);
if (s == -3 && abs(r) == 1) s = r;
if (s * r == -1) return false;
}
return true;
}
int main() {
int N;
cin >> N;
Poly poly(N);
for (int i = 0; i < N; i++) {
double x, y;
cin >> x >> y;
poly[i] = P(x, y);
}
if(is_convex(poly)) cout << 1 << endl;
else cout << 0<< endl;
}
|
#include <iostream>
#include <fstream>
#include <typeinfo>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <iomanip>
#include <cctype>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<25;
const double pi=acos(-1);
const double eps=1e-8;
const vi emp;
struct point{
double x,y;
point operator+(point p){
return point{x+p.x,y+p.y};
}
point operator-(point p){
return point{x-p.x,y-p.y};
}
point operator*(double p){
return point{x*p,y*p};
}
point operator/(double p){
if(!p) return point{0,0};
return point{x/p,y/p};
}
bool operator==(point p){
return fabs(x-p.x)<eps&&fabs(y-p.y)<eps;
}
};
typedef pair<point,point> pp;
typedef vector<point> VP;
const point O{0,0};
class Geom{
public:
double Length(point x,point y){
point z=y-x;
return sqrt(z.x*z.x+z.y*z.y);
}
double IP(point p,point q){
return p.x*q.x+p.y*q.y;
}
double CP(point p,point q){
return p.x*q.y-q.x*p.y;
}
string Counter_Clockwise(pp a,point x){
point A=a.second-a.first;
point X=x-a.first;
double ip=IP(A,X),cp=CP(A,X),Al=Length(O,A),Xl=Length(O,X);
if(cp>eps) return "Counter_Clockwise";
if(cp<-eps) return "Clockwise";
if(ip<-eps) return "Online_Back";
if(Xl<Al||fabs(Xl-Al)<eps) return "On_Segment";
return "Online_Front";
}
double Heron(point A,point B,point C){
double a=Length(B,C),b=Length(C,A),c=Length(A,B),s=(a+b+c)/2;
return sqrt(s*(s-a)*(s-b)*(s-c));
}
double Area(VP p){
double sum;
string s;
p.push_back(p[0]);
for(VP::iterator i=p.begin()+1;i!=p.end();i++){
s=Counter_Clockwise(pp(O,*(i-1)),*i);
if(s[1]=='o') sum+=Heron(O,*(i-1),*i);
else if(s[1]=='l') sum-=Heron(O,*(i-1),*i);
}
return sum;
}
string Convex_Concave(VP p){
p.push_back(p[0]);
string s;
for(VP::iterator i=p.begin()+1;i!=p.end()-1;i++){
s=Counter_Clockwise(pp(*(i-1),*i),*(i+1));
if(s[1]=='l') return "Concave";
}
return "Convex";
}
void Point_in(point& p){
cin>>p.x>>p.y;
}
};
int N;
VP p;
int main(){
cout<<syosu(1);
Geom geo;
cin>>N;
p=VP(N);
for(VP::iterator i=p.begin();i!=p.end();i++) geo.Point_in(*i);
string s=geo.Convex_Concave(p);
if(s=="Convex") cout<<1<<endl;
else cout<<0<<endl;
} |
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <algorithm>
#include <array>
#include <complex>
#include <string>
#include <utility>
#include <map>
#include <queue>
#include <list>
#include <functional>
#include <numeric>
#include <stack>
#include <tuple>
using namespace std;
int dx[4] = { -1,0,1,0 };
int dy[4] = { 0,1,0,-1 };
const int INF = 100000000;
const long long LINF = 1000000000000000000;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-6;
using pii = std::pair<int, int>;
using ll = long long;
using pLL = std::pair<ll, ll>;
#define SORT(v) std::sort(v.begin(), v.end())
#define RSORT(v) std::sort(v.rbegin(), v.rend())
using Point = complex<double>; //点
using Line = pair<Point, Point>; //直線
using Poly = vector<Point>;
#define X real() //実部
#define Y imag() //虚部
double dot(Point a, Point b) { //内積
return (a.X * b.X + a.Y * b.Y);
}
double cross(Point a, Point b) { //外積
return a.X * b.Y - a.Y * b.X;
}
Point Projection(Line s, Point p) { //点から直線への射影
Point vase = s.second - s.first;
p -= s.first;
return s.first + dot(p, vase) / norm(vase) * vase;
}
Point Reflection(Line s, Point p) { //直線に対して線対称な点
return p + (Projection(s, p) - p) * 2.0;
}
int ccw(Point a, Point b, Point c) { //直線に対する点の位置
if (cross(b - a, c - a) > EPS)return 1;
if (cross(b - a, c - a) < -EPS)return -1;
if (dot(b - a, c - a) < -EPS)return 2;
if (abs(b - a) + EPS < abs(c - a))return -2;
return 0;
}
bool isOrthogonal(Line a, Line b) { //直行判定
return dot(a.second - a.first, b.second - b.first) ? 0 : 1;
}
bool isParallel(Line a, Line b) { //平行判定
return cross(a.second - a.first, b.second - b.first) ? 0 : 1;
}
bool isCross(Line a, Line b) { //交差判定
if (ccw(a.first, a.second, b.first) * ccw(a.first, a.second, b.second) <= 0 and
ccw(b.first, b.second, a.first) * ccw(b.first, b.second, a.second) <= 0) {
return true;
}
return false;
}
Point CrossPoint(Line a, Line b) { //直線の交点
double x, d;
Point p1, p2, p3, p4;
p1 = a.first;
p2 = a.second;
p3 = b.first;
p4 = b.second;
x = (p4.Y - p3.Y) * (p4.X - p1.X) - (p4.X - p3.X) * (p4.Y - p1.Y);
d = (p4.Y - p3.Y) * (p2.X - p1.X) - (p4.X - p3.X) * (p2.Y - p1.Y);
return p1 + x / d * (p2 - p1);
}
double PointLineDis(Line l, Point p) {
Point a = l.first, b = l.second, c = p;
return abs(cross(c - a, b - a)) / abs(b - a);
}
double PointSegDis(Line a, Point p) {
Point p1 = a.first, p2 = a.second;
if (dot(p2 - p1, p - p1) < EPS)return abs(p - p1);
if (dot(p1 - p2, p - p2) < EPS)return abs(p - p2);
return PointLineDis(a, p);
}
double SegSegDis(Line a, Line b) {
if (isCross(a, b)) return 0;
double res = PointSegDis(a, b.first);
res = min(res, PointSegDis(a, b.second));
res = min(res, PointSegDis(b, a.first));
res = min(res, PointSegDis(b, a.second));
return res;
}
double PolyArea(const Poly &p) {
if (p.size() < 3)return 0;
double res = cross(p[p.size() - 1], p[0]);
for (size_t i = 0; i < p.size()-1; ++i) {
res += cross(p[i], p[i + 1]);
}
return res / 2.0;
}
bool isConvex(const Poly &p) {
for (size_t i = 0; i < p.size(); ++i) {
if (ccw(p[i],p[(i+1)%p.size()], p[(i + 2) % p.size()]) ==-1)return false;
}
return true;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
Point p1, p2, p3, p4;
int q;
cin >> q;
Poly p;
for (int i = 0; i < q; ++i) {
double x1,y1;
cin >> x1 >> y1;
p1 = Point(x1, y1);
p.push_back(p1);
}
if (isConvex(p))cout << 1 << endl;
else cout << 0 << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
using namespace std;
typedef bool B;
typedef long double D;
typedef complex<D> P;
typedef vector<P> VP;
typedef struct {P s,t;} L;
typedef vector<L> VL;
typedef struct {P c;D r;} C;
typedef vector <C> VC;
const D eps=1.0e-10;
const D pi=acos(-1.0);
template<class T> bool operator==(T a, T b){return abs(a-b)< eps;}
template<class T> bool operator< (T a, T b){return a < b-eps;}
template<class T> bool operator<=(T a, T b){return a < b+eps;}
template<class T> int sig(T r) {return (r==0||r==-0) ? 0 : r > 0 ? 1 : -1;}
#define X real()
#define Y imag()
D ip(P a, P b) {return a.X * b.X + a.Y * b.Y;}
D ep(P a, P b) {return a.X * b.Y - a.Y * b.X;}
D sq(D a) {return sqrt(max(a, (D)0));}
P vec(L l){return l.t-l.s;}
inline P input(){D x,y;cin >> x >> y; return P(x,y);}
// ???????????¬????????¨???
// ???????????? verify AOJ CGL_1_C
enum CCW{
LEFT = 1,
RIGHT = 2,
BACK = 4,
FRONT = 8,
MID = 16,
ON=FRONT|BACK|MID
};
inline int ccw(P base, P a, P b) { //???a??¨???b???????????????????????????
a -= base; b -= base;
if (ep(a, b) > 0)
return LEFT; // counter clockwise
if (ep(a, b) < 0)
return RIGHT; // clockwise
if (ip(a, b) < 0)
return BACK; // b--base--a on line
if (norm(a) < norm(b))
return FRONT; // base--a--b on line
// otherwise
return MID; // base--b--a on line a??¨b????????????????????????
}
// ??????????????????
inline B cmp_x(const P &a,const P &b){
return (abs(a.X-b.X)<eps ) ? a.Y<b.Y : a.X<b.X;
} // base x
inline B cmp_y(const P &a,const P &b){
return (abs(a.Y-b.Y)<eps ) ? a.X<b.X : a.Y<b.Y;
} // base y
inline B cmp_a(const P &a,const P &b){
return (abs(arg(a)-arg(b))<eps ) ? norm(a) < norm(b) : arg(a)<arg(b);
} // base arg
// ????§???¢
// Area
// Verify AOJ 1100
D area(VP pol){
int n=pol.size();
D sum=0.0;
rep(i,n){
D x=pol[i%n].X-pol[(i+1)%n].X;
D y=pol[i%n].Y+pol[(i+1)%n].Y;
sum+=x*y;
}
return abs(sum/2.0);
}
B is_convex(VP pol){
int n=pol.size();
rep(i,n){
P prev=pol[(i+n-1)%n];
P next=pol[(i+1)%n];
if(ccw(prev,pol[i],next)==RIGHT) return false;
}
return true;
}
// Convex_hull
// Verify AOJ 0063
VP convex_hull(VP pol){
int n=pol.size(),k=0;
sort(pol.begin(),pol.end(),cmp_x);
VP res(2*n);
// down
rep(i,n){
//????§???¢????????????????????????????????´??????<0????????´???????????¨
while( k>1 && ep(res[k-1]-res[k-2],pol[i]-res[k-1])<0) k--;
res[k++]=pol[i];
}
// up
for(int i=n-2,t=k;i>=0;i--){
//????§???¢????????????????????????????????´??????<0????????´???????????¨
while( k>t && ep(res[k-1]-res[k-2],pol[i]-res[k-1])<0) k--;
res[k++]=pol[i];
}
res.resize(k-1);
return res;
}
// ????§???¢??????????????????(???????§???¢??????)
//verify AOJ CGL_3-C
int in_polygon(VP pol,P p){
int n=pol.size();
int res=0;
rep(i,n){
if(ccw(pol[i],pol[(i+1)%n],p)==MID)
return 1;
D vt=(p.Y-pol[i].Y)/(pol[(i+1)%n].Y-pol[i].Y);
D dx=pol[(i+1)%n].X-pol[i].X;
if((pol[i].Y<=p.Y)&&(p.Y< pol[(i+1)%n].Y)&&(p.X<pol[i].X+vt*dx))res++;
if((pol[i].Y> p.Y)&&(p.Y>=pol[(i+1)%n].Y)&&(p.X<pol[i].X+vt*dx))res--;
}
return res?2:0;
}
int main(void){
int n;
cin >> n;
VP pol;
rep(i,n) pol.push_back(input());
cout << is_convex(pol) << endl;
return 0;
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.