Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <meta http-equiv="content-type" content="text/html; charset=UTF-8">
- <title>Fast Rect Intersection Test</title>
- <style type="text/css">
- #debug {
- width: 100%;
- height: 50px;
- position: fixed;
- background: #ffffa0;
- }
- .box {
- position: absolute;
- top: 120px;
- left: 120px;
- width: 50px;
- height: 50px;
- border: 1px solid #aaa;
- }
- </style>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
- <script type="text/javascript">
- <!-- //
- ;
- function Rec(ob)
- {
- this.lf = ob.offset().left;
- this.tp = ob.offset().top;
- this.rt = this.lf + ob.width();
- this.bt = this.tp + ob.height();
- // this is te core of implementation
- // a scalar test
- this.checkCollision = function( rec )
- {
- if (this.bt >= rec.tp )
- if (this.tp <= rec.bt)
- if (this.rt >= rec.lf)
- if (this.lf <= rec.rt)
- return true;
- return false;
- };
- this.toString = function()
- {
- return "["+ this.lf + "," + this.rt + "," + this.tp + "," + this.bt + "," +"]"
- };
- return this;
- }
- function mouseMove(e)
- {
- var s = $("#seeker");
- s.css("top", e.pageY - s.height()*.5).css("left", e.pageX - s.width()*.5);
- var r = new Rec(s);
- $("#debug").html( String((r.checkCollision(r2)) ? "true" : "false") + " " + e.pageX + " " + e.pageY + " " + r.toString());
- }
- var r2;
- function ini()
- {
- r2 = new Rec($("#col1"));
- $(window).mousemove(mouseMove);
- }
- $(window).ready(ini);
- // -->
- </script>
- </head>
- <body>
- <div id="debug"></div>
- <div class="box" id="col1"></div>
- <div class="box" id="seeker"></div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement