Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Wei Wei
cs349_w18_examples
Commits
4e969ad9
Commit
4e969ad9
authored
Feb 05, 2018
by
Keiko Katsuragawa
Browse files
add some comments
parent
2105231e
Changes
1
Hide whitespace changes
Inline
Side-by-side
java/2-9-HitTest/TransformHittest.java
View file @
4e969ad9
...
...
@@ -31,8 +31,8 @@ class Canvas extends JComponent {
Point
M
=
new
Point
();
// mouse point
AffineTransform
AT1
;
AffineTransform
AT2
;
AffineTransform
AT1
;
// transform matrix for shape1
AffineTransform
AT2
;
// transform matrix for shape2
// the house shape (model position is centred at top left corner)
...
...
@@ -47,13 +47,12 @@ class Canvas extends JComponent {
Canvas
()
{
// create transformation matri
ces
// create transformation matri
x for shape1
AT1
=
new
AffineTransform
();
AT1
.
translate
(
350
,
100
);
AT1
.
rotate
(
Math
.
toRadians
(
30
));
AT1
.
scale
(
1
,
1
);
// create another transformation matri
ces
// create another transformation matri
x for shape2
AT2
=
new
AffineTransform
();
AT2
.
translate
(
200
,
300
);
AT2
.
rotate
(
Math
.
toRadians
(
30
));
...
...
@@ -84,47 +83,50 @@ class Canvas extends JComponent {
g2
.
drawPolygon
(
shape
.
xpoints
,
shape
.
ypoints
,
shape
.
npoints
);
//
Transformed
Shape
1
// Shape1
g2
.
setTransform
(
AT1
);
// Use Transform Matrix AT1 for shape1
g2
.
setColor
(
Color
.
RED
);
g2
.
drawPolygon
(
shape
.
xpoints
,
shape
.
ypoints
,
shape
.
npoints
);
// hit test
// hit testing
Point
MT
=
new
Point
();
try
{
// create an inverse matrix of AT1
AffineTransform
IAT1
=
AT1
.
createInverse
();
// apply the inverse transformation to the mouse position
IAT1
.
transform
(
M
,
MT
);
// check if original shape contains transformed mouse position
if
(
shape
.
contains
(
MT
.
x
,
MT
.
y
))
g2
.
setColor
(
Color
.
RED
);
else
g2
.
setColor
(
Color
.
WHITE
);
g2
.
fillPolygon
(
shape
);
}
catch
(
NoninvertibleTransformException
e
){
// error
}
g2
.
fillPolygon
(
shape
);
// Transformed Shape 1
// Shape2
g2
.
setTransform
(
AT2
);
// Use Transform Matrix AT2 for shape2
g2
.
setColor
(
Color
.
BLUE
);
g2
.
drawPolygon
(
shape
.
xpoints
,
shape
.
ypoints
,
shape
.
npoints
);
// hit test
try
{
// create an inverse matrix of AT2
AffineTransform
IAT2
=
AT2
.
createInverse
();
// apply the inverse transformation to the mouse position
IAT2
.
transform
(
M
,
MT
);
// check if original shape contains transformed mouse position
if
(
shape
.
contains
(
MT
.
x
,
MT
.
y
))
g2
.
setColor
(
Color
.
BLUE
);
else
g2
.
setColor
(
Color
.
WHITE
);
g2
.
fillPolygon
(
shape
);
}
catch
(
NoninvertibleTransformException
e
){
// error
}
g2
.
fillPolygon
(
shape
);
// reset to transform
g2
.
setTransform
(
ATG
);
}
}
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