gitee.com/quant1x/num@v0.3.2/slope_test.go (about)

     1  package num
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  	"testing"
     7  )
     8  
     9  func TestLine_basic(t *testing.T) {
    10  	degrees := 90.00
    11  	slope := DegreesToSlope(degrees)
    12  	fmt.Println(slope)
    13  	degrees = SlopeToDegrees(slope)
    14  	fmt.Println(degrees)
    15  }
    16  
    17  func TestLineEquation(t *testing.T) {
    18  	p1 := Point{0, 3}
    19  	p2 := Point{4, 0}
    20  
    21  	line := CalculateLineEquation(p2, p1)
    22  	fmt.Println(line)
    23  	r1 := line.Radian()
    24  	d1 := radianToDegrees(r1)
    25  	d2 := 90 - math.Abs(d1)
    26  	r2 := degreesToRadian(d2)
    27  	b := p1.Y/math.Sin(math.Abs(r1)) == p2.X/math.Sin(r2)
    28  	fmt.Println(b)
    29  	d3 := 90.00
    30  	r3 := degreesToRadian(d3)
    31  	radio := p1.Y / math.Sin(math.Abs(r1))
    32  	hypotenuse := radio * math.Sin(r3)
    33  	fmt.Println(hypotenuse)
    34  }
    35  
    36  func TestSlope(t *testing.T) {
    37  	a := math.Nextafter(2.0, 3.0)
    38  	t.Logf("a = %f", a)
    39  
    40  	x1 := 0
    41  	y1 := 5.00
    42  	x2 := 3
    43  	y2 := 10.00
    44  	xl := Slope(x1, y1, x2, y2)
    45  	t.Logf("xl = %f", xl)
    46  
    47  	x3 := 6
    48  	y3 := xl*float64(x3-x2) + y2
    49  	t.Logf("y3 = %f", y3)
    50  }
    51  
    52  func TestLine_Degrees(t *testing.T) {
    53  	type fields struct {
    54  		slope     float64
    55  		intercept float64
    56  	}
    57  	tests := []struct {
    58  		name   string
    59  		fields fields
    60  		want   float64
    61  	}{
    62  		{
    63  			name:   "45°",
    64  			fields: fields{slope: 1, intercept: 1},
    65  			want:   45,
    66  		},
    67  	}
    68  	for _, tt := range tests {
    69  		t.Run(tt.name, func(t *testing.T) {
    70  			this := Line{
    71  				slope:     tt.fields.slope,
    72  				intercept: tt.fields.intercept,
    73  			}
    74  			if got := this.Degrees(); got != tt.want {
    75  				t.Errorf("Degrees() = %v, want %v", got, tt.want)
    76  			}
    77  		})
    78  	}
    79  }
    80  
    81  // 计算点 (x0, y0) 到直线 Ax + By + C = 0 的距离
    82  func distancePointLine(A, B, C float64, x0, y0 float64) float64 {
    83  	return math.Abs(A*x0+B*y0+C) / math.Sqrt(A*A+B*B)
    84  }
    85  
    86  func TestLine_Distance_basic(t *testing.T) {
    87  	A, B, C := 1.0, -1.0, 2.0 // 直线的系数,例如 A=1, B=-1, C=2 对应于方程 x - y + 2 = 0
    88  	x0, y0 := 3.0, 4.0        // 点的坐标
    89  	d := distancePointLine(A, B, C, x0, y0)
    90  	fmt.Printf("The distance from point (%.2f, %.2f) to the line is %.2f\n", x0, y0, d)
    91  }
    92  
    93  func TestLine_Distance(t *testing.T) {
    94  	type fields struct {
    95  		slope     float64
    96  		intercept float64
    97  	}
    98  	type args struct {
    99  		p Point
   100  	}
   101  	tests := []struct {
   102  		name   string
   103  		fields fields
   104  		args   args
   105  		want   float64
   106  	}{
   107  		{
   108  			name:   "d1",
   109  			fields: fields{slope: -1, intercept: -2},
   110  			args:   args{p: Point{4, 5}},
   111  			want:   -7.7781745930520225,
   112  		},
   113  		{
   114  			name:   "d2",
   115  			fields: fields{slope: 2, intercept: 3},
   116  			args:   args{p: Point{4, 5}},
   117  			want:   2.6832815729997477,
   118  		},
   119  	}
   120  	for _, tt := range tests {
   121  		t.Run(tt.name, func(t *testing.T) {
   122  			this := Line{
   123  				slope:     tt.fields.slope,
   124  				intercept: tt.fields.intercept,
   125  			}
   126  			if got := this.Distance(tt.args.p); got != tt.want {
   127  				t.Errorf("Distance() = %v, want %v", got, tt.want)
   128  			}
   129  		})
   130  	}
   131  }
   132  
   133  func TestSlopeToDegrees(t *testing.T) {
   134  	type args struct {
   135  		m float64
   136  	}
   137  	tests := []struct {
   138  		name string
   139  		args args
   140  		want float64
   141  	}{
   142  		{
   143  			name: "t1",
   144  			args: args{m: 0.5},
   145  			want: 26.56505117707799,
   146  		},
   147  		{
   148  			name: "t2",
   149  			args: args{m: 1},
   150  			want: 45,
   151  		},
   152  		{
   153  			name: "t3",
   154  			args: args{m: 0},
   155  			want: 0.0,
   156  		},
   157  		{
   158  			name: "t4",
   159  			args: args{m: 1.6331239353195392e+16},
   160  			want: 90,
   161  		},
   162  	}
   163  	for _, tt := range tests {
   164  		t.Run(tt.name, func(t *testing.T) {
   165  			if got := SlopeToDegrees(tt.args.m); got != tt.want {
   166  				t.Errorf("SlopeToDegrees() = %v, want %v", got, tt.want)
   167  			}
   168  		})
   169  	}
   170  }
   171  
   172  func TestDegreesToSlope(t *testing.T) {
   173  	type args struct {
   174  		x float64
   175  	}
   176  	tests := []struct {
   177  		name string
   178  		args args
   179  		want float64
   180  	}{
   181  		{
   182  			name: "t1",
   183  			args: args{x: 45},
   184  			want: 1,
   185  		},
   186  	}
   187  	for _, tt := range tests {
   188  		t.Run(tt.name, func(t *testing.T) {
   189  			if got := DegreesToSlope(tt.args.x); got != tt.want {
   190  				t.Errorf("DegreesToSlope() = %v, want %v", got, tt.want)
   191  			}
   192  		})
   193  	}
   194  }
   195  
   196  func TestLine_Angle(t *testing.T) {
   197  	type fields struct {
   198  		slope     float64
   199  		intercept float64
   200  		offset    float64
   201  	}
   202  	type args struct {
   203  		other Line
   204  	}
   205  	tests := []struct {
   206  		name   string
   207  		fields fields
   208  		args   args
   209  		want   float64
   210  	}{
   211  		{
   212  			name:   "t1",
   213  			fields: fields{slope: 0},
   214  			args:   args{other: Line{slope: 1}},
   215  			want:   45.00,
   216  		},
   217  		{
   218  			name:   "t2",
   219  			fields: fields{slope: 1},
   220  			args:   args{other: Line{slope: 0}},
   221  			want:   -45,
   222  		},
   223  		{
   224  			name:   "t0",
   225  			fields: fields{slope: 1},
   226  			args:   args{other: Line{slope: -1}},
   227  			want:   0,
   228  		},
   229  	}
   230  	for _, tt := range tests {
   231  		t.Run(tt.name, func(t *testing.T) {
   232  			this := Line{
   233  				slope:     tt.fields.slope,
   234  				intercept: tt.fields.intercept,
   235  				offset:    tt.fields.offset,
   236  			}
   237  			if got := this.Angle(tt.args.other); got != tt.want {
   238  				t.Errorf("Angle() = %v, want %v", got, tt.want)
   239  			}
   240  		})
   241  	}
   242  }