github.com/searKing/golang/go@v1.2.117/image/rectangle_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  	"fmt"
     9  	"testing"
    10  
    11  	image_ "github.com/searKing/golang/go/image"
    12  )
    13  
    14  func TestRectangle2f(t *testing.T) {
    15  	// in checks that every point in f is in g.
    16  	in := func(f, g image_.Rectangle2f) error {
    17  		if !f.In(g) {
    18  			return fmt.Errorf("f=%s, f.In(%s): got false, want true", f, g)
    19  		}
    20  		for y := f.Min.Y; y < f.Max.Y; y++ {
    21  			for x := f.Min.X; x < f.Max.X; x++ {
    22  				p := image_.Point2f{x, y}
    23  				if !p.In(g) {
    24  					return fmt.Errorf("p=%s, p.In(%s): got false, want true", p, g)
    25  				}
    26  			}
    27  		}
    28  		return nil
    29  	}
    30  
    31  	rects := []image_.Rectangle2f{
    32  		image_.Rect2f(0, 0, 10, 10),
    33  		image_.Rect2f(10, 0, 20, 10),
    34  		image_.Rect2f(1, 2, 3, 4),
    35  		image_.Rect2f(4, 6, 10, 10),
    36  		image_.Rect2f(2, 3, 12, 5),
    37  		image_.Rect2f(-1, -2, 0, 0),
    38  		image_.Rect2f(-1, -2, 4, 6),
    39  		image_.Rect2f(-10, -20, 30, 40),
    40  		image_.Rect2f(8, 8, 8, 8),
    41  		image_.Rect2f(88, 88, 88, 88),
    42  		image_.Rect2f(6, 5, 4, 3),
    43  	}
    44  
    45  	// r.Eq(s) should be equivalent to every point in r being in s, and every
    46  	// point in s being in r.
    47  	for _, r := range rects {
    48  		for _, s := range rects {
    49  			got := r.Eq(s)
    50  			want := in(r, s) == nil && in(s, r) == nil
    51  			if got != want {
    52  				t.Errorf("Eq: r=%s, s=%s: got %t, want %t", r, s, got, want)
    53  			}
    54  		}
    55  	}
    56  
    57  	// The intersection should be the largest rectangle a such that every point
    58  	// in a is both in r and in s.
    59  	for _, r := range rects {
    60  		for _, s := range rects {
    61  			a := r.Intersect(s)
    62  			if err := in(a, r); err != nil {
    63  				t.Errorf("Intersect: r=%s, s=%s, a=%s, a not in r: %v", r, s, a, err)
    64  			}
    65  			if err := in(a, s); err != nil {
    66  				t.Errorf("Intersect: r=%s, s=%s, a=%s, a not in s: %v", r, s, a, err)
    67  			}
    68  			if isZero, overlaps := a == (image_.Rectangle2f{}), r.Overlaps(s); isZero == overlaps {
    69  				t.Errorf("Intersect: r=%s, s=%s, a=%s: isZero=%t same as overlaps=%t",
    70  					r, s, a, isZero, overlaps)
    71  			}
    72  			largerThanA := [4]image_.Rectangle2f{a, a, a, a}
    73  			largerThanA[0].Min.X--
    74  			largerThanA[1].Min.Y--
    75  			largerThanA[2].Max.X++
    76  			largerThanA[3].Max.Y++
    77  			for i, b := range largerThanA {
    78  				if b.Empty() {
    79  					// b isn't actually larger than a.
    80  					continue
    81  				}
    82  				if in(b, r) == nil && in(b, s) == nil {
    83  					t.Errorf("Intersect: r=%s, s=%s, a=%s, b=%s, i=%d: intersection could be larger",
    84  						r, s, a, b, i)
    85  				}
    86  			}
    87  		}
    88  	}
    89  
    90  	// The union should be the smallest rectangle a such that every point in r
    91  	// is in a and every point in s is in a.
    92  	for _, r := range rects {
    93  		for _, s := range rects {
    94  			a := r.Union(s)
    95  			if err := in(r, a); err != nil {
    96  				t.Errorf("Union: r=%s, s=%s, a=%s, r not in a: %v", r, s, a, err)
    97  			}
    98  			if err := in(s, a); err != nil {
    99  				t.Errorf("Union: r=%s, s=%s, a=%s, s not in a: %v", r, s, a, err)
   100  			}
   101  			if a.Empty() {
   102  				// You can't get any smaller than a.
   103  				continue
   104  			}
   105  			smallerThanA := [4]image_.Rectangle2f{a, a, a, a}
   106  			smallerThanA[0].Min.X++
   107  			smallerThanA[1].Min.Y++
   108  			smallerThanA[2].Max.X--
   109  			smallerThanA[3].Max.Y--
   110  			for i, b := range smallerThanA {
   111  				if in(r, b) == nil && in(s, b) == nil {
   112  					t.Errorf("Union: r=%s, s=%s, a=%s, b=%s, i=%d: union could be smaller",
   113  						r, s, a, b, i)
   114  				}
   115  			}
   116  		}
   117  	}
   118  }