Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Georgii Aksenov
2021-pb162-seminar-project
Commits
2bc653f9
Commit
2bc653f9
authored
Mar 15, 2021
by
Georgii Aksenov
Browse files
Iteration-02
parent
e9d91a5a
Pipeline
#69710
passed with stage
in 10 seconds
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
src/main/java/cz/muni/fi/pb162/project/Demo.java
→
src/main/java/cz/muni/fi/pb162/project/
demo/
Demo.java
View file @
2bc653f9
package
cz.muni.fi.pb162.project
;
package
cz.muni.fi.pb162.project
.demo
;
import
cz.muni.fi.pb162.project.geometry.Triangle
;
import
cz.muni.fi.pb162.project.geometry.Vertex2D
;
import
cz.muni.fi.pb162.project.geometry.Vertex2D
;
/**
/**
...
@@ -16,26 +17,11 @@ public class Demo {
...
@@ -16,26 +17,11 @@ public class Demo {
* @param args command line arguments, will be ignored
* @param args command line arguments, will be ignored
*/
*/
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
Vertex2D
v1
=
newVertex
(
2
,
3
);
Triangle
triangle
=
new
Triangle
(
Vertex2D
v2
=
newVertex
(
1
,
1
);
new
Vertex2D
(-
100.0
,
0
),
new
Vertex2D
(
0
,
100
),
new
Vertex2D
(
100
,
-
100
));
v1
.
move
(
v2
);
System
.
out
.
println
(
triangle
.
toString
());
System
.
out
.
println
(
v1
.
getInfo
());
System
.
out
.
println
(
v2
.
getInfo
());
}
/**
* Creates new instance of Vertex2D.
*
* @param x coordinate for new vector
* @param y coordinate for new vector
* @return new Vertex2D.
*/
private
static
Vertex2D
newVertex
(
double
x
,
double
y
)
{
Vertex2D
vertex
=
new
Vertex2D
();
vertex
.
setX
(
x
);
vertex
.
setY
(
y
);
return
vertex
;
}
}
}
}
src/main/java/cz/muni/fi/pb162/project/demo/Draw.java
View file @
2bc653f9
...
@@ -104,4 +104,4 @@ public final class Draw extends JFrame {
...
@@ -104,4 +104,4 @@ public final class Draw extends JFrame {
int
a2
=
(
int
)
Math
.
rint
(
HALF_HEIGHT
-
triangle
.
getVertex
(
index
).
getY
());
int
a2
=
(
int
)
Math
.
rint
(
HALF_HEIGHT
-
triangle
.
getVertex
(
index
).
getY
());
return
new
AbstractMap
.
SimpleEntry
<>(
a1
,
a2
);
return
new
AbstractMap
.
SimpleEntry
<>(
a1
,
a2
);
}
}
}
}
\ No newline at end of file
src/main/java/cz/muni/fi/pb162/project/geometry/Triangle.java
0 → 100644
View file @
2bc653f9
package
cz.muni.fi.pb162.project.geometry
;
import
static
java
.
util
.
Arrays
.
stream
;
import
static
java
.
util
.
stream
.
Collectors
.
joining
;
/**
* This class represents a triangle in 2D space.
*
* @author Georgii Aksenov <xaksenov@fi.muni.cz>
*/
public
class
Triangle
{
private
final
Vertex2D
[]
vertices
;
private
final
Triangle
[]
subTriangles
=
new
Triangle
[
3
];
/**
* Checkstyle asks for the Javadoc here.
* This is a constructor.
* It sets corners ¯\_(ツ)_/¯
*
* @param v1 is the first corner
* @param v2 is the second corner
* @param v3 is the third corner
*/
public
Triangle
(
Vertex2D
v1
,
Vertex2D
v2
,
Vertex2D
v3
)
{
this
.
vertices
=
new
Vertex2D
[]{
v1
,
v2
,
v3
};
}
/**
* Returns the index-th vertex if the index is in range 0..2, otherwise null
*
* @param index is the index of the vector
* @return the index-th vertex
*/
public
Vertex2D
getVertex
(
int
index
)
{
if
(
index
<
0
||
index
>
2
)
{
return
null
;
}
return
vertices
[
index
];
}
/**
* Sets the index-th vertex (if the index is in range 0..2)
*
* @param index is the index of the vector to be set
* @param vertex to be set
*/
public
void
setVertex
(
int
index
,
Vertex2D
vertex
)
{
if
(!
isValid
(
index
))
{
return
;
}
vertices
[
index
]
=
vertex
;
}
@Override
public
String
toString
()
{
return
"Triangle: vertices="
+
stream
(
vertices
)
.
map
(
Vertex2D:
:
toString
)
.
collect
(
joining
(
" "
));
}
/**
* Sets inner triangles for this triangle,
* each of them is built by one of the corners of the triangle
* and the middle between this corner and the other.
*
* @return false if triangle was already divided, otherwise true
*/
public
boolean
divide
()
{
if
(
isDivided
())
{
return
false
;
}
subTriangles
[
0
]
=
createSubTriangle
(
0
,
1
,
2
);
subTriangles
[
1
]
=
createSubTriangle
(
1
,
0
,
2
);
subTriangles
[
2
]
=
createSubTriangle
(
2
,
0
,
1
);
return
true
;
}
/**
* @return true if the triangle was already divided.
*/
public
boolean
isDivided
()
{
return
subTriangles
[
0
]
!=
null
;
}
/**
* Returns the index-th subtriangle if the index is in range 0..2, otherwise null
*
* @param index is the index of the vector
* @return the index-th subtriangle
*/
public
Triangle
getSubTriangle
(
int
index
)
{
if
(!
isValid
(
index
)
||
!
isDivided
())
{
return
null
;
}
return
subTriangles
[
index
];
}
private
Triangle
createSubTriangle
(
int
cornerIndex
,
int
oppositeIndex1
,
int
oppositeIndex2
)
{
Vertex2D
corner
=
vertices
[
cornerIndex
];
Vertex2D
opposite1
=
vertices
[
oppositeIndex1
];
Vertex2D
opposite2
=
vertices
[
oppositeIndex2
];
return
new
Triangle
(
corner
,
corner
.
createMiddle
(
opposite1
),
corner
.
createMiddle
(
opposite2
));
}
private
boolean
isValid
(
int
index
)
{
return
index
>=
0
&&
index
<=
2
;
}
}
src/main/java/cz/muni/fi/pb162/project/geometry/Vertex2D.java
View file @
2bc653f9
package
cz.muni.fi.pb162.project.geometry
;
package
cz.muni.fi.pb162.project.geometry
;
import
static
java
.
lang
.
String
.
format
;
/**
/**
* This class represents a point in 2D space.
*
* @author Georgii Aksenov <xaksenov@fi.muni.cz>
* @author Georgii Aksenov <xaksenov@fi.muni.cz>
*/
*/
public
class
Vertex2D
{
public
class
Vertex2D
{
private
double
x
=
0
;
private
final
double
x
;
private
double
y
=
0
;
private
final
double
y
;
public
double
getX
()
{
/**
return
x
;
* Checkstyle asks for the Javadoc here.
* This is a constructor.
* It sets coordinates ¯\_(ツ)_/¯
*
* @param x is the x coordinate
* @param y is the y coordinate
*/
public
Vertex2D
(
double
x
,
double
y
)
{
this
.
x
=
x
;
this
.
y
=
y
;
}
}
public
void
setX
(
double
x
)
{
public
double
getX
(
)
{
this
.
x
=
x
;
return
x
;
}
}
public
double
getY
()
{
public
double
getY
()
{
return
y
;
return
y
;
}
}
public
void
setY
(
double
y
)
{
this
.
y
=
y
;
}
/**
/**
* To get string representation.
* To get string representation.
*
*
* @return string representation.
* @return string representation.
*/
*/
public
String
getInfo
()
{
@Override
return
String
.
format
(
"[%.1f, %.1f]"
,
x
,
y
);
public
String
toString
()
{
}
return
format
(
"[%s, %s]"
,
Double
.
valueOf
(
x
).
toString
(),
Double
.
valueOf
(
y
).
toString
());
/**
* To get sum of coordinates.
*
* @return sum of coordinates.
*/
public
double
sumCoordinates
()
{
return
x
+
y
;
}
}
/**
/**
*
Moves the vertex
.
*
To get string representation
.
*
*
* @param vertex is added to this vertex.
* @param opposite the opposite point to create middle against.
* @return string representation.
*/
*/
public
void
move
(
Vertex2D
vertex
)
{
public
Vertex2D
createMiddle
(
Vertex2D
opposite
)
{
x
+=
vertex
.
x
;
return
new
Vertex2D
((
x
+
opposite
.
x
)
/
2
,
(
y
+
opposite
.
y
)
/
2
);
y
+=
vertex
.
y
;
}
}
}
}
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment