首页 > 编程语言 >从一个坐标点移动到另一个坐标点,每次移动x,y最大举例不能超过127 , 算法

从一个坐标点移动到另一个坐标点,每次移动x,y最大举例不能超过127 , 算法

时间:2023-03-09 11:13:17浏览次数:33  
标签:正数 args KVMMouseControl 绝对值 127 model 移动 坐标

我的思路是这样的,先移正方形距离,再移横向或者纵向距离,最终移动边边角角的距离

代码如下

type KVMMouseControl struct {
	KeyState uint8  `json:"keyState"` // 按键状态  按键状态  0x00:按键抬起 0x01:左键按下 0x02:右键按下 0x04:中键按下
	X        uint16 `json:"x"`        // X 坐标
	Y        uint16 `json:"y"`        // Y 坐标
	Wheel    uint8  `json:"wheel"`    // 滚轮变化 滚轮变化 0x00:未动 0x01:向上滚动 0xFF:向下滚动
}

// getMaxVal 根据 bool 返回正值或者负值
func getMaxVal(moreThanZero bool, maxVal int) int8 {
	if moreThanZero {
		return int8(maxVal)
	}
	return int8(-maxVal)
}

// offsetX x 偏移距离 可为负 ,offsetY y轴偏移距离 可为负  ,maxVal 最大移动距离限制 
func getControlList(offsetX int, offsetY int, maxVal int) []model.KVMMouseControl {
	var controls []model.KVMMouseControl
	absX := math.Abs(float64(offsetX))
	offsetXMoreThanZero := offsetX >= 0
	absY := math.Abs(float64(offsetY))
	offsetYMoreThanZero := offsetY >= 0
	// absX 和 absY 都小于127,直接构造一个 tlv 协议 返回
	if int(absX) < maxVal && int(absY) < maxVal {
		var tlv model.KVMMouseControl
		tlv.X = uint16(offsetX)
		tlv.Y = uint16(offsetY)
		controls = append(controls, tlv)
		return controls
	}
	offsetXNumbers := int(math.Ceil(absX / float64(maxVal)))
	offsetXLastWidth := offsetX % maxVal
	offsetYNumbers := int(math.Ceil(absY / float64(maxVal)))
	offsetYLastWidth := offsetY % maxVal
	maxNumbers := offsetXNumbers
	minNumbers := offsetYNumbers - 1
	if offsetXNumbers < offsetYNumbers {
		maxNumbers = offsetYNumbers
		minNumbers = offsetXNumbers - 1
	}
	for i := 1; i <= maxNumbers; i++ {
		var tlv model.KVMMouseControl
		if i <= minNumbers {
			tlv.X = getMaxVal(offsetXMoreThanZero, maxVal)
			tlv.Y = getMaxVal(offsetYMoreThanZero, maxVal)
			controls = append(controls, tlv)
			continue
		}
		if i == maxNumbers {
			tlv.X = int8(offsetXLastWidth)
			tlv.Y = int8(offsetYLastWidth)
			controls = append(controls, tlv)
			continue
		}
		// x周比y轴宽
		if offsetXNumbers > offsetYNumbers {
			tlv.X = getMaxVal(offsetXMoreThanZero, maxVal)
		} else {
			tlv.Y = getMaxVal(offsetYMoreThanZero, maxVal)
		}
		controls = append(controls, tlv)
	}
	return controls
}
单元测试 
func Test_getControlList(t *testing.T) {

	type args struct {
		offsetX int
		offsetY int
		maxVal  int
	}
	tests := []struct {
		name string
		args args
		want []model.KVMMouseControl
	}{
		{"全是正数并且绝对值小于127", args{1, 1, 127}, []model.KVMMouseControl{{X: 1, Y: 1}}},
		{"x正数y负数绝对值都小于127", args{1, -1, 127}, []model.KVMMouseControl{{X: 1, Y: -1}}},
		{"x负数y正数绝对值都小于127", args{-1, 1, 127}, []model.KVMMouseControl{{X: -1, Y: 1}}},
		{"x负数y负数绝对值都小于127", args{-1, -1, 127}, []model.KVMMouseControl{{X: -1, Y: -1}}},
		{"x正数y正数绝对值都大于127", args{128, 128, 127}, []model.KVMMouseControl{{X: 127, Y: 127}, {X: 1, Y: 1}}},
		{"x正数y负数绝对值都大于127", args{128, -128, 127}, []model.KVMMouseControl{{X: 127, Y: -127}, {X: 1, Y: -1}}},
		{"x负数y正数绝对值都大于127", args{-128, 128, 127}, []model.KVMMouseControl{{X: -127, Y: 127}, {X: -1, Y: 1}}},
		{"x负数y负数绝对值都大于127", args{-128, -128, 127}, []model.KVMMouseControl{{X: -127, Y: -127}, {X: -1, Y: -1}}},
		{"x正数y正绝对值都大与256", args{257, 257, 127}, []model.KVMMouseControl{{X: 127, Y: 127}, {X: 127, Y: 127}, {X: 3, Y: 3}}},
		{"x正数y负数绝对值都大与256", args{257, -257, 127}, []model.KVMMouseControl{{X: 127, Y: -127}, {X: 127, Y: -127}, {X: 3, Y: -3}}},
		{"x负数y正数绝对值都大与256", args{-257, 257, 127}, []model.KVMMouseControl{{X: -127, Y: 127}, {X: -127, Y: 127}, {X: -3, Y: 3}}},
		{"x负数y负数绝对值都大与256", args{-257, -257, 127}, []model.KVMMouseControl{{X: -127, Y: -127}, {X: -127, Y: -127}, {X: -3, Y: -3}}},
		{"x正数y正数,x绝对值大于256,y绝对值大于127", args{257, 128, 127}, []model.KVMMouseControl{{X: 127, Y: 127}, {X: 127, Y: 0}, {X: 3, Y: 1}}},
		{"x正数y正数,x绝对值大于127,y绝对值大于256", args{128, 257, 127}, []model.KVMMouseControl{{X: 127, Y: 127}, {X: 0, Y: 127}, {X: 1, Y: 3}}},
		{"x正数y正数,x绝对值大于512,y绝对值大于127", args{513, 128, 127}, []model.KVMMouseControl{{X: 127, Y: 127}, {X: 127, Y: 0}, {X: 127, Y: 0}, {X: 127, Y: 0}, {X: 5, Y: 1}}},
		{"x正数y正数,x绝对值大于127,y绝对值大于512", args{128, 513, 127}, []model.KVMMouseControl{{X: 127, Y: 127}, {X: 0, Y: 127}, {X: 0, Y: 127}, {X: 0, Y: 127}, {X: 1, Y: 5}}},
		{"x正数y正数,x绝对值大于127,y绝对值大于512", args{128, 513, 127}, []model.KVMMouseControl{{X: 127, Y: 127}, {X: 0, Y: 127}, {X: 0, Y: 127}, {X: 0, Y: 127}, {X: 1, Y: 5}}},
		{"x正数y正数,x绝对值大于512,y绝对值大于512", args{513, 513, 127}, []model.KVMMouseControl{{X: 127, Y: 127}, {X: 127, Y: 127}, {X: 127, Y: 127}, {X: 127, Y: 127}, {X: 5, Y: 5}}},
		{"x正数y负数,x绝对值大于256,y绝对值大于127", args{257, -128, 127}, []model.KVMMouseControl{{X: 127, Y: -127}, {X: 127, Y: 0}, {X: 3, Y: -1}}},
		{"x正数y负数,x绝对值大于127,y绝对值大于256", args{128, -257, 127}, []model.KVMMouseControl{{X: 127, Y: -127}, {X: 0, Y: -127}, {X: 1, Y: -3}}},

		{"x正数y负数,x绝对值大于512,y绝对值大于127", args{513, -128, 127}, []model.KVMMouseControl{{X: 127, Y: -127}, {X: 127, Y: 0}, {X: 127, Y: 0}, {X: 127, Y: 0}, {X: 5, Y: -1}}},
		{"x正数y负数,x绝对值大于127,y绝对值大于512", args{128, -513, 127}, []model.KVMMouseControl{{X: 127, Y: -127}, {X: 0, Y: -127}, {X: 0, Y: -127}, {X: 0, Y: -127}, {X: 1, Y: -5}}},
		{"x正数y负数,x绝对值大于127,y绝对值大于512", args{128, -513, 127}, []model.KVMMouseControl{{X: 127, Y: -127}, {X: 0, Y: -127}, {X: 0, Y: -127}, {X: 0, Y: -127}, {X: 1, Y: -5}}},
		{"x正数y负数,x绝对值大于512,y绝对值大于512", args{513, -513, 127}, []model.KVMMouseControl{{X: 127, Y: -127}, {X: 127, Y: -127}, {X: 127, Y: -127}, {X: 127, Y: -127}, {X: 5, Y: -5}}},
		{"x负数y正数,x绝对值大于256,y绝对值大于127", args{-257, 128, 127}, []model.KVMMouseControl{{X: -127, Y: 127}, {X: -127, Y: 0}, {X: -3, Y: 1}}},
		{"x负数y正数,x绝对值大于127,y绝对值大于256", args{-128, 257, 127}, []model.KVMMouseControl{{X: -127, Y: 127}, {X: 0, Y: 127}, {X: -1, Y: 3}}},
		{"x负数y正数,x绝对值大于512,y绝对值大于127", args{-513, 128, 127}, []model.KVMMouseControl{{X: -127, Y: 127}, {X: -127, Y: 0}, {X: -127, Y: 0}, {X: -127, Y: 0}, {X: -5, Y: 1}}},
		{"x负数y正数,x绝对值大于127,y绝对值大于512", args{-128, 513, 127}, []model.KVMMouseControl{{X: -127, Y: 127}, {X: 0, Y: 127}, {X: 0, Y: 127}, {X: 0, Y: 127}, {X: -1, Y: 5}}},

		{"x负数y正数,x绝对值大于512,y绝对值大于512", args{-513, 513, 127}, []model.KVMMouseControl{{X: -127, Y: 127}, {X: -127, Y: 127}, {X: -127, Y: 127}, {X: -127, Y: 127}, {X: -5, Y: 5}}},
		{"x负数y负数,x绝对值大于256,y绝对值大于127", args{-257, -128, 127}, []model.KVMMouseControl{{X: -127, Y: -127}, {X: -127, Y: 0}, {X: -3, Y: -1}}},
		{"x负数y负数,x绝对值大于256,y绝对值大于127", args{-257, -128, 127}, []model.KVMMouseControl{{X: -127, Y: -127}, {X: -127, Y: 0}, {X: -3, Y: -1}}},
		{"x负数y负数,x绝对值大于127,y绝对值大于256", args{-128, -257, 127}, []model.KVMMouseControl{{X: -127, Y: -127}, {X: 0, Y: -127}, {X: -1, Y: -3}}},
		{"x负数y负数,x绝对值大于512,y绝对值大于127", args{-513, -128, 127}, []model.KVMMouseControl{{X: -127, Y: -127}, {X: -127, Y: 0}, {X: -127, Y: 0}, {X: -127, Y: 0}, {X: -5, Y: -1}}},
		{"x负数y负数,x绝对值大于127,y绝对值大于512", args{-128, -513, 127}, []model.KVMMouseControl{{X: -127, Y: -127}, {X: 0, Y: -127}, {X: 0, Y: -127}, {X: 0, Y: -127}, {X: -1, Y: -5}}},
		{"x负数y负数,x绝对值大于512,y绝对值大于512", args{-513, -513, 127}, []model.KVMMouseControl{{X: -127, Y: -127}, {X: -127, Y: -127}, {X: -127, Y: -127}, {X: -127, Y: -127}, {X: -5, Y: -5}}},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := getControlList(tt.args.offsetX, tt.args.offsetY, tt.args.maxVal); !reflect.DeepEqual(got, tt.want) {
				t.Errorf("\ngot =%+v,\nwant=%+v", got, tt.want)
			}
		})
	}

单元测试通过

标签:正数,args,KVMMouseControl,绝对值,127,model,移动,坐标
From: https://www.cnblogs.com/ifnk/p/17197617.html

相关文章