github.com/searKing/golang/go@v1.2.117/image/geom_test.go (about) 1 // Copyright 2022 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package image_test 6 7 import ( 8 "image" 9 "testing" 10 11 image_ "github.com/searKing/golang/go/image" 12 ) 13 14 func TestScaleRectangleBySize(t *testing.T) { 15 tests := []struct { 16 rect image.Rectangle 17 size image.Point 18 limit image.Rectangle 19 wantRect image.Rectangle 20 }{ 21 { 22 rect: image.Rect(0, 0, 10, 10), 23 size: image.Pt(5, 5), 24 limit: image.Rect(0, 0, 10, 10), 25 wantRect: image.Rect(3, 3, 8, 8), 26 }, 27 { 28 rect: image.Rect(0, 0, 10, 10), 29 size: image.Pt(5, 5), 30 limit: image.Rect(0, 4, 7, 10), 31 wantRect: image.Rect(2, 4, 7, 9), 32 }, 33 { 34 rect: image.Rect(0, 0, 10, 10), 35 size: image.Pt(5, 5), 36 limit: image.Rect(4, -8, 8, -4), 37 wantRect: image.Rect(4, -8, 8, -4), 38 }, 39 { 40 rect: image.Rect(0, 0, 10, 10), 41 size: image.Pt(50, 50), 42 limit: image.Rect(-100, -100, 100, 100), 43 wantRect: image.Rect(-20, -20, 30, 30), 44 }, 45 { 46 rect: image.Rect(0, 0, 10, 10), 47 size: image.Pt(5, 5), 48 limit: image.Rect(5, 5, 10, 10), 49 wantRect: image.Rect(5, 5, 10, 10), 50 }, 51 } 52 53 for i, tt := range tests { 54 gotRect := image_.ScaleRectangleBySize(tt.rect, tt.size, tt.limit) 55 if !tt.wantRect.Eq(gotRect) { 56 t.Errorf("#%d: expected %s got %s", i, tt.wantRect, gotRect) 57 } 58 } 59 } 60 61 func TestRectangle2f_ScaleByFactor(t *testing.T) { 62 tests := []struct { 63 rect image_.Rectangle2f 64 factor image_.Point2f 65 wantRect image_.Rectangle2f 66 }{ 67 { 68 rect: image_.Rect2f(10, 10, 10, 10), 69 factor: image_.Pt2f(1, 1), 70 wantRect: image_.Rect2f(10, 10, 10, 10), 71 }, 72 { 73 rect: image_.Rect2f(0, 0, 10, 10), 74 factor: image_.Pt2f(1, 1), 75 wantRect: image_.Rect2f(0, 0, 10, 10), 76 }, 77 { 78 rect: image_.Rect2f(0, 0, 10, 10), 79 factor: image_.Pt2f(-1, -1), 80 wantRect: image_.Rect2f(10, 10, 0, 0), 81 }, 82 { 83 rect: image_.Rect2f(0, 0, 10, 10), 84 factor: image_.Pt2f(2, 0.5), 85 wantRect: image_.Rect2f(-5, 2.5, 15, 7.5), 86 }, 87 { 88 rect: image_.Rect2f(0, 0, 10, 10), 89 factor: image_.Pt2f(-2, -0.5), 90 wantRect: image_.Rect2f(15, 7.5, -5, 2.5), 91 }, 92 { 93 rect: image_.Rect2f(-10, -10, 20, 20), 94 factor: image_.Pt2f(0, 0), 95 wantRect: image_.Rect2f(5, 5, 5, 5), 96 }, 97 } 98 99 for i, tt := range tests { 100 gotRect := tt.rect.ScaleByFactor(tt.factor) 101 if !tt.wantRect.RoundRectangle().Eq(gotRect.RoundRectangle()) { 102 t.Errorf("#%d: expected %s got %s", i, tt.wantRect, gotRect) 103 } 104 } 105 } 106 107 func TestRectangle2f_UnionPoints(t *testing.T) { 108 tests := []struct { 109 rect image_.Rectangle2f 110 pts []image_.Point2f 111 wantRect image_.Rectangle2f 112 }{ 113 { 114 rect: image_.Rect2f(0, 0, 0, 0), 115 pts: []image_.Point2f{image_.Pt2f(2, 2)}, 116 wantRect: image_.Rect2f(2, 2, 2, 2), 117 }, 118 { 119 rect: image_.Rect2f(0, 0, 10, 10), 120 pts: []image_.Point2f{}, 121 wantRect: image_.Rect2f(0, 0, 10, 10), 122 }, 123 { 124 rect: image_.Rect2f(0, 0, 10, 10), 125 pts: []image_.Point2f{image_.Pt2f(1, 1)}, 126 wantRect: image_.Rect2f(0, 0, 10, 10), 127 }, 128 { 129 rect: image_.Rect2f(0, 0, 10, 10), 130 pts: []image_.Point2f{image_.Pt2f(-1, -1)}, 131 wantRect: image_.Rect2f(-1, -1, 10, 10), 132 }, 133 { 134 rect: image_.Rect2f(0, 0, 10, 10), 135 pts: []image_.Point2f{image_.Pt2f(20, 20)}, 136 wantRect: image_.Rect2f(0, 0, 20, 20), 137 }, 138 { 139 rect: image_.Rect2f(0, 0, 10, 10), 140 pts: []image_.Point2f{image_.Pt2f(-1, 1)}, 141 wantRect: image_.Rect2f(-1, 0, 10, 10), 142 }, 143 { 144 rect: image_.Rect2f(0, 0, 10, 10), 145 pts: []image_.Point2f{image_.Pt2f(-1, 20)}, 146 wantRect: image_.Rect2f(-1, 0, 10, 20), 147 }, 148 { 149 rect: image_.Rect2f(0, 0, 10, 10), 150 pts: []image_.Point2f{image_.Pt2f(1, 1), image_.Pt2f(-1, -1), image_.Pt2f(20, 20)}, 151 wantRect: image_.Rect2f(-1, -1, 20, 20), 152 }, 153 } 154 155 for i, tt := range tests { 156 gotRect := tt.rect.UnionPoints(tt.pts...) 157 if !tt.wantRect.RoundRectangle().Eq(gotRect.RoundRectangle()) { 158 t.Errorf("#%d: %v.UnionPoints(%v) = %v, want %v", i, tt.rect, tt.pts, gotRect, tt.wantRect) 159 } 160 } 161 }