...

Source file src/oss.terrastruct.com/d2/lib/geo/orientation.go

Documentation: oss.terrastruct.com/d2/lib/geo

     1  package geo
     2  
     3  type Orientation int
     4  
     5  const (
     6  	TopLeft Orientation = iota
     7  	TopRight
     8  	BottomLeft
     9  	BottomRight
    10  
    11  	Top
    12  	Right
    13  	Bottom
    14  	Left
    15  
    16  	NONE
    17  )
    18  
    19  func (o Orientation) ToString() string {
    20  	switch o {
    21  	case TopLeft:
    22  		return "TopLeft"
    23  	case TopRight:
    24  		return "TopRight"
    25  	case BottomLeft:
    26  		return "BottomLeft"
    27  	case BottomRight:
    28  		return "BottomRight"
    29  
    30  	case Top:
    31  		return "Top"
    32  	case Right:
    33  		return "Right"
    34  	case Bottom:
    35  		return "Bottom"
    36  	case Left:
    37  		return "Left"
    38  	default:
    39  		return ""
    40  	}
    41  }
    42  
    43  func (o1 Orientation) SameSide(o2 Orientation) bool {
    44  	sides := [][]Orientation{
    45  		{TopLeft, Top, TopRight},
    46  		{BottomLeft, Bottom, BottomRight},
    47  		{Left, TopLeft, BottomLeft},
    48  		{Right, TopRight, BottomRight},
    49  	}
    50  	for _, sameSides := range sides {
    51  		isO1 := false
    52  		for _, side := range sameSides {
    53  			if side == o1 {
    54  				isO1 = true
    55  				break
    56  			}
    57  		}
    58  		if isO1 {
    59  			for _, side := range sameSides {
    60  				if side == o2 {
    61  					return true
    62  				}
    63  			}
    64  		}
    65  	}
    66  	return false
    67  }
    68  
    69  func (o Orientation) IsDiagonal() bool {
    70  	return o == TopLeft || o == TopRight || o == BottomLeft || o == BottomRight
    71  }
    72  
    73  func (o Orientation) IsHorizontal() bool {
    74  	return o == Left || o == Right
    75  }
    76  
    77  func (o Orientation) IsVertical() bool {
    78  	return o == Top || o == Bottom
    79  }
    80  
    81  func (o Orientation) GetOpposite() Orientation {
    82  	switch o {
    83  	case TopLeft:
    84  		return BottomRight
    85  	case TopRight:
    86  		return BottomLeft
    87  	case BottomLeft:
    88  		return TopRight
    89  	case BottomRight:
    90  		return TopLeft
    91  
    92  	case Top:
    93  		return Bottom
    94  	case Bottom:
    95  		return Top
    96  	case Right:
    97  		return Left
    98  	case Left:
    99  		return Right
   100  
   101  	default:
   102  		return o
   103  	}
   104  }
   105  

View as plain text