1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 'use strict'; 6 7 base.require('base.quad'); 8 base.require('base.rect'); 9 10 base.unittest.testSuite('base.quad', function() { 11 test('pointInTri', function() { 12 var res = base.vecInTriangle2( 13 [0.25, 0.25], 14 [0, 0], 15 [1, 0], 16 [0, 1]); 17 assertTrue(res); 18 }); 19 20 test('pointNotInTri', function() { 21 var res = base.vecInTriangle2( 22 [0.75, 0.75], 23 [0, 0], 24 [1, 0], 25 [0, 1]); 26 assertFalse(res); 27 }); 28 29 test('pointInside', function() { 30 var q = base.Quad.From4Vecs([0, 0], 31 [1, 0], 32 [1, 1], 33 [0, 1]); 34 var res = q.vecInside([0.5, 0.5]); 35 assertTrue(res); 36 }); 37 38 test('pointNotInQuad', function() { 39 var q = base.Quad.From4Vecs([0, 0], 40 [1, 0], 41 [1, 1], 42 [0, 1]); 43 var res = q.vecInside([1.5, 0.5]); 44 assertFalse(res); 45 }); 46 47 test('isRectangle', function() { 48 assertTrue(base.Quad.FromXYWH(0, 0, 10, 10).isRectangle()); 49 assertTrue(base.Quad.FromXYWH(-10, -10, 5, 5).isRectangle()); 50 assertTrue(base.Quad.FromXYWH(-10, -10, 20, 20).isRectangle()); 51 assertTrue(base.Quad.FromXYWH(-10, 10, 5, 5).isRectangle()); 52 53 assertFalse(base.Quad.FromXYWH(0, 0, -10, -10).isRectangle()); 54 assertFalse(base.Quad.From8Array([0, 1, 2, 3, 4, 5, 6, 7]).isRectangle()); 55 assertFalse(base.Quad.From8Array([0, 0, 0, 5, 5, 5, 0, 0]).isRectangle()); 56 }); 57 58 test('projectUnitRect', function() { 59 var container = base.Quad.FromXYWH(0, 0, 10, 10); 60 var srcRect = base.Rect.FromXYWH(0.1, 0.8, 0.8, 0.1); 61 var expectedRect = srcRect.scale(10); 62 63 var q = new base.Quad(); 64 container.projectUnitRectFast(q, srcRect); 65 66 assertQuadEquals(base.Quad.FromRect(expectedRect), q); 67 }); 68 69 test('projectUnitRectOntoUnitQuad', function() { 70 var container = base.Quad.FromXYWH(0, 0, 1, 1); 71 var srcRect = base.Rect.FromXYWH(0.0, 0, 1, 1); 72 var expectedRect = srcRect; 73 74 var q = new base.Quad(); 75 container.projectUnitRectFast(q, srcRect); 76 77 assertQuadEquals(base.Quad.FromRect(expectedRect), q); 78 }); 79 80 test('projectUnitRectOntoSizeTwoQuad', function() { 81 var container = base.Quad.FromXYWH(0, 0, 2, 2); 82 var srcRect = base.Rect.FromXYWH(0.0, 0, 1, 1); 83 var expectedRect = srcRect.scale(2); 84 85 var q = new base.Quad(); 86 container.projectUnitRectFast(q, srcRect); 87 88 assertQuadEquals(base.Quad.FromRect(expectedRect), q); 89 }); 90 91 test('projectUnitRectOntoTranslatedQuad', function() { 92 var container = base.Quad.FromXYWH(1, 1, 1, 1); 93 var srcRect = base.Rect.FromXYWH(0.0, 0, 1, 1); 94 var expectedRect = srcRect.translate([1, 1]); 95 96 var q = new base.Quad(); 97 container.projectUnitRectFast(q, srcRect); 98 99 assertQuadEquals(base.Quad.FromRect(expectedRect), q); 100 }); 101 102 test('projectShrunkUnitRectOntoUnitQuad', function() { 103 var container = base.Quad.FromXYWH(0, 0, 1, 1); 104 var srcRect = base.Rect.FromXYWH(0.1, 0.1, 0.8, 0.8); 105 var expectedRect = srcRect; 106 107 var q = new base.Quad(); 108 container.projectUnitRectFast(q, srcRect); 109 110 assertQuadEquals(base.Quad.FromRect(expectedRect), q); 111 }); 112 }); 113