Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
competitive
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Harbor Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bruno Freitas Tissei
competitive
Commits
cbeab170
Commit
cbeab170
authored
6 years ago
by
Bruno Freitas Tissei
Browse files
Options
Downloads
Patches
Plain Diff
Add geometry functions and minor fixes
Signed-off-by:
Bruno Freitas Tissei
<
bft15@inf.ufpr.br
>
parent
f1d7f03d
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
algorithms/geometry/convex_hull.cpp
+4
-4
4 additions, 4 deletions
algorithms/geometry/convex_hull.cpp
algorithms/geometry/geometry_functions.cpp
+113
-10
113 additions, 10 deletions
algorithms/geometry/geometry_functions.cpp
algorithms/graph/lca.cpp
+1
-1
1 addition, 1 deletion
algorithms/graph/lca.cpp
with
118 additions
and
15 deletions
algorithms/geometry/convex_hull.cpp
+
4
−
4
View file @
cbeab170
...
@@ -23,21 +23,21 @@ struct ConvexHull {
...
@@ -23,21 +23,21 @@ struct ConvexHull {
*/
*/
int
run
(
const
vector
<
dd
>
&
v
)
{
int
run
(
const
vector
<
dd
>
&
v
)
{
int
k
=
0
;
int
k
=
0
;
vector
<
int
>
ans
(
v
.
s
z
*
2
);
vector
<
int
>
ans
(
v
.
s
ize
()
*
2
);
sort
(
v
.
begin
(),
v
.
end
(
),
[](
const
dd
&
a
,
const
dd
&
b
)
{
sort
(
all
(
v
),
[](
const
dd
&
a
,
const
dd
&
b
)
{
return
(
a
.
fi
==
b
.
fi
)
?
(
a
.
se
<
b
.
se
)
:
(
a
.
fi
<
b
.
fi
);
return
(
a
.
fi
==
b
.
fi
)
?
(
a
.
se
<
b
.
se
)
:
(
a
.
fi
<
b
.
fi
);
});
});
// Uppermost part of convex hull
// Uppermost part of convex hull
for
(
int
i
=
0
;
i
<
v
.
s
z
;
++
i
)
{
for
(
int
i
=
0
;
i
<
v
.
s
ize
()
;
++
i
)
{
while
(
k
>=
2
&&
cross
(
v
[
ans
[
k
-
2
]],
v
[
ans
[
k
-
1
]],
v
[
i
])
<
0
)
while
(
k
>=
2
&&
cross
(
v
[
ans
[
k
-
2
]],
v
[
ans
[
k
-
1
]],
v
[
i
])
<
0
)
k
--
;
k
--
;
ans
[
k
++
]
=
i
;
ans
[
k
++
]
=
i
;
}
}
// Lowermost part of convex hull
// Lowermost part of convex hull
for
(
int
i
=
v
.
s
z
-
2
,
t
=
k
+
1
;
i
>=
0
;
--
i
)
{
for
(
int
i
=
v
.
s
ize
()
-
2
,
t
=
k
+
1
;
i
>=
0
;
--
i
)
{
while
(
k
>=
t
&&
cross
(
v
[
ans
[
k
-
2
]],
v
[
ans
[
k
-
1
]],
v
[
i
])
<
0
)
while
(
k
>=
t
&&
cross
(
v
[
ans
[
k
-
2
]],
v
[
ans
[
k
-
1
]],
v
[
i
])
<
0
)
k
--
;
k
--
;
ans
[
k
++
]
=
i
;
ans
[
k
++
]
=
i
;
...
...
This diff is collapsed.
Click to expand it.
algorithms/geometry/geometry_functions.cpp
+
113
−
10
View file @
cbeab170
...
@@ -2,21 +2,124 @@
...
@@ -2,21 +2,124 @@
* Geometry functions
* Geometry functions
*/
*/
struct
Vector
{
template
<
typename
T
>
double
x
,
y
;
struct
Point
{
T
x
,
y
;
Vector
(
double
x
,
double
y
)
:
x
(
x
),
y
(
y
)
{}
Point
(
T
x
,
T
y
)
:
x
(
x
),
y
(
y
)
{}
double
dot_product
(
Vector
v
)
{
return
x
*
v
.
x
+
y
*
v
.
y
;
}
Point
operator
+
(
Point
p
)
{
return
Point
(
x
+
p
.
x
,
y
+
p
.
y
);
}
double
cross_product
(
Vector
v
)
{
return
x
*
v
.
y
-
v
.
x
*
y
;
}
Point
operator
-
(
Point
p
)
{
return
Point
(
x
-
p
.
x
,
y
-
p
.
y
);
}
T
dot
(
Point
p
)
{
return
(
x
*
p
.
x
)
+
(
y
*
p
.
y
);
}
T
cross
(
Point
p
)
{
return
(
x
*
p
.
y
)
-
(
y
*
p
.
y
);
}
/**
* Returns angle between this and p:
* atan2(y, x) is in the range [-180,180]. To get [0, 360],
* atan2(-y, -x) + 180 is used
*/
T
angle
(
Point
p
)
{
return
((
atan2
(
-
cross
(
p
),
-
dot
(
p
))
*
180.0
)
/
M_PI
)
+
180.0
;
}
/**
* Returns cosine value between this and p.
*/
T
cosine
(
Point
p
)
{
return
(
dot
(
p
)
/
(
sqrt
(
dot
(
*
this
))
*
sqrt
(
p
.
dot
(
p
))));
}
/**
* Returns sine value between this and p.
*/
T
sine
(
Point
p
)
{
return
(
cross
(
p
)
/
(
sqrt
(
dot
(
*
this
))
*
sqrt
(
p
.
dot
(
p
))));
}
/**
* Finds orientation of ordered triplet (a,b,c).
* 0 - Colinear;
* 1 - Clockwise;
* 2 - Counterclockwise.
*/
static
int
orientation
(
Point
a
,
Point
b
,
Point
c
)
{
T
val
=
(
b
-
a
).
cross
(
c
-
b
);
if
(
val
==
0
)
return
0
;
return
(
val
>
0
)
?
1
:
2
;
}
};
template
<
typename
T
>
struct
Segment
{
Point
<
T
>
a
,
b
;
Segment
(
Point
a
,
Point
b
)
:
a
(
a
),
b
(
b
)
{}
/**
* Checks if points p and q are on the same side of the segment.
*/
bool
same_side
(
Point
p
,
Point
q
)
{
T
cpp
=
(
p
-
a
).
cross
(
b
-
a
);
T
cpq
=
(
q
-
a
).
cross
(
b
-
a
);
return
((
cpp
>
0
&&
cpq
>
0
)
||
(
cpp
<
0
&&
cpq
<
0
));
}
/**
* Checks if point p is on the segment.
*/
bool
on_segment
(
Point
p
)
{
return
(
p
.
x
<=
max
(
a
.
x
,
b
.
x
)
&&
p
.
x
>=
min
(
a
.
x
,
b
.
x
)
&&
p
.
y
<=
max
(
a
.
y
,
b
.
y
)
&&
p
.
y
>=
min
(
a
.
y
,
b
.
y
));
}
/**
* Checks if segment intersects with s.
*/
bool
intersect
(
Segment
s
)
{
int
o1
=
Point
::
orientation
(
a
,
b
,
s
.
a
);
int
o2
=
Point
::
orientation
(
a
,
b
,
s
.
b
);
int
o3
=
Point
::
orientation
(
s
.
a
,
s
.
b
,
a
);
int
o4
=
Point
::
orientation
(
s
.
a
,
s
.
b
,
b
);
if
(
o1
!=
o2
&&
o3
!=
o4
)
return
true
;
if
(
o1
==
0
&&
on_segment
(
s
.
a
))
return
true
;
if
(
o2
==
0
&&
on_segment
(
s
.
b
))
return
true
;
if
(
o3
==
0
&&
s
.
on_segment
(
a
))
return
true
;
if
(
o4
==
0
&&
s
.
on_segment
(
b
))
return
true
;
return
false
;
}
};
template
<
typename
T
>
struct
Polygon
{
vector
<
Point
<
T
>>
vertices
;
Polygon
()
{}
Polygon
(
vector
<
Point
>
vertices
)
:
vertices
(
vertices
)
{}
/**
* Adds a vertex to the polygon.
*/
void
add_point
(
Point
p
)
{
vertices
.
pb
(
p
);
}
/**
/**
* Returns angle between this and v.
* Returns area of polygon (only works when vertices are sorted in clockwise
* or counterclockwise order).
*/
*/
double
angle
(
Vector
v
)
{
double
area
()
{
double
ans
=
0
;
for
(
int
i
=
0
;
i
<
vertices
.
size
();
++
i
)
ans
+=
vertices
[
i
].
cross
(
vertices
[(
i
+
1
)
%
vertices
.
size
()]);
// atan2(y, x) is in the range [-180,180]. To get [0, 360],
return
fabs
(
ans
)
/
2.0
;
// atan2(-y, -x) + 180 is used
return
((
atan2
(
-
cross_product
(
v
),
-
dot_product
(
v
))
*
180.0
)
/
M_PI
)
+
180.0
;
}
}
};
};
This diff is collapsed.
Click to expand it.
algorithms/graph/lca.cpp
+
1
−
1
View file @
cbeab170
...
@@ -50,7 +50,7 @@ struct LCA {
...
@@ -50,7 +50,7 @@ struct LCA {
*/
*/
void
preprocess
(
int
v
)
{
void
preprocess
(
int
v
)
{
mset
(
par
,
-
1
);
mset
(
par
,
-
1
);
//*** mset(cost, 0;
//*** mset(cost, 0
)
;
dfs
(
v
);
dfs
(
v
);
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment