Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cs349_w18_examples
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container 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
Sam Kantor
cs349_w18_examples
Commits
4e969ad9
Commit
4e969ad9
authored
7 years ago
by
Keiko Katsuragawa
Browse files
Options
Downloads
Patches
Plain Diff
add some comments
parent
2105231e
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
java/2-9-HitTest/TransformHittest.java
+16
-14
16 additions, 14 deletions
java/2-9-HitTest/TransformHittest.java
with
16 additions
and
14 deletions
java/2-9-HitTest/TransformHittest.java
+
16
−
14
View file @
4e969ad9
...
@@ -31,8 +31,8 @@ class Canvas extends JComponent {
...
@@ -31,8 +31,8 @@ class Canvas extends JComponent {
Point
M
=
new
Point
();
// mouse point
Point
M
=
new
Point
();
// mouse point
AffineTransform
AT1
;
AffineTransform
AT1
;
// transform matrix for shape1
AffineTransform
AT2
;
AffineTransform
AT2
;
// transform matrix for shape2
// the house shape (model position is centred at top left corner)
// the house shape (model position is centred at top left corner)
...
@@ -47,13 +47,12 @@ class Canvas extends JComponent {
...
@@ -47,13 +47,12 @@ class Canvas extends JComponent {
Canvas
()
{
Canvas
()
{
// create transformation matri
ces
// create transformation matri
x for shape1
AT1
=
new
AffineTransform
();
AT1
=
new
AffineTransform
();
AT1
.
translate
(
350
,
100
);
AT1
.
translate
(
350
,
100
);
AT1
.
rotate
(
Math
.
toRadians
(
30
));
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
=
new
AffineTransform
();
AT2
.
translate
(
200
,
300
);
AT2
.
translate
(
200
,
300
);
AT2
.
rotate
(
Math
.
toRadians
(
30
));
AT2
.
rotate
(
Math
.
toRadians
(
30
));
...
@@ -84,47 +83,50 @@ class Canvas extends JComponent {
...
@@ -84,47 +83,50 @@ class Canvas extends JComponent {
g2
.
drawPolygon
(
shape
.
xpoints
,
shape
.
ypoints
,
shape
.
npoints
);
g2
.
drawPolygon
(
shape
.
xpoints
,
shape
.
ypoints
,
shape
.
npoints
);
//
Transformed
Shape
1
// Shape1
g2
.
setTransform
(
AT1
);
// Use Transform Matrix AT1 for shape1
g2
.
setTransform
(
AT1
);
// Use Transform Matrix AT1 for shape1
g2
.
setColor
(
Color
.
RED
);
g2
.
setColor
(
Color
.
RED
);
g2
.
drawPolygon
(
shape
.
xpoints
,
shape
.
ypoints
,
shape
.
npoints
);
g2
.
drawPolygon
(
shape
.
xpoints
,
shape
.
ypoints
,
shape
.
npoints
);
// hit test
// hit testing
Point
MT
=
new
Point
();
Point
MT
=
new
Point
();
try
{
try
{
// create an inverse matrix of AT1
AffineTransform
IAT1
=
AT1
.
createInverse
();
AffineTransform
IAT1
=
AT1
.
createInverse
();
// apply the inverse transformation to the mouse position
IAT1
.
transform
(
M
,
MT
);
IAT1
.
transform
(
M
,
MT
);
// check if original shape contains transformed mouse position
if
(
shape
.
contains
(
MT
.
x
,
MT
.
y
))
if
(
shape
.
contains
(
MT
.
x
,
MT
.
y
))
g2
.
setColor
(
Color
.
RED
);
g2
.
setColor
(
Color
.
RED
);
else
else
g2
.
setColor
(
Color
.
WHITE
);
g2
.
setColor
(
Color
.
WHITE
);
g2
.
fillPolygon
(
shape
);
}
catch
(
NoninvertibleTransformException
e
){
}
catch
(
NoninvertibleTransformException
e
){
// error
// error
}
}
g2
.
fillPolygon
(
shape
);
// Shape2
// Transformed Shape 1
g2
.
setTransform
(
AT2
);
// Use Transform Matrix AT2 for shape2
g2
.
setTransform
(
AT2
);
// Use Transform Matrix AT2 for shape2
g2
.
setColor
(
Color
.
BLUE
);
g2
.
setColor
(
Color
.
BLUE
);
g2
.
drawPolygon
(
shape
.
xpoints
,
shape
.
ypoints
,
shape
.
npoints
);
g2
.
drawPolygon
(
shape
.
xpoints
,
shape
.
ypoints
,
shape
.
npoints
);
// hit test
// hit test
try
{
try
{
// create an inverse matrix of AT2
AffineTransform
IAT2
=
AT2
.
createInverse
();
AffineTransform
IAT2
=
AT2
.
createInverse
();
// apply the inverse transformation to the mouse position
IAT2
.
transform
(
M
,
MT
);
IAT2
.
transform
(
M
,
MT
);
// check if original shape contains transformed mouse position
if
(
shape
.
contains
(
MT
.
x
,
MT
.
y
))
if
(
shape
.
contains
(
MT
.
x
,
MT
.
y
))
g2
.
setColor
(
Color
.
BLUE
);
g2
.
setColor
(
Color
.
BLUE
);
else
else
g2
.
setColor
(
Color
.
WHITE
);
g2
.
setColor
(
Color
.
WHITE
);
g2
.
fillPolygon
(
shape
);
}
catch
(
NoninvertibleTransformException
e
){
}
catch
(
NoninvertibleTransformException
e
){
// error
// error
}
}
g2
.
fillPolygon
(
shape
);
// reset to transform
// reset to transform
g2
.
setTransform
(
ATG
);
g2
.
setTransform
(
ATG
);
}
}
}
}
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