diff --git a/src/main/java/org/cloud/sonic/driver/common/tool/PocoXYTransformer.java b/src/main/java/org/cloud/sonic/driver/common/tool/PocoXYTransformer.java new file mode 100644 index 0000000..99a71f0 --- /dev/null +++ b/src/main/java/org/cloud/sonic/driver/common/tool/PocoXYTransformer.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) [SonicCloudOrg] Sonic Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.cloud.sonic.driver.common.tool; + +/*** + * poco coordinate system conversion and vertical coordinate system converter + */ +public class PocoXYTransformer { + /** + * Convert adb vertical coordinates to poco coordinate system coordinates according to different directions + * + * @param x x poco coordinate point x + * @param y y poco coordinate point y + * @param w w ScreenWidth + * @param h h ScreenHeight + * @param orientation The device orientation, based on the vertical direction of the mobile device, + * rotate 90° counterclockwise, orientation=90, 180° counterclockwise, orientation=180, + * and so on + * @return {@link int[]} + */ + public static double[] PocoTransformerVertical(double x, double y, double w, double h, Integer orientation) { + if (orientation == 90) { + double temp = x; + x = w - y; + y = temp; + } else if (orientation == 180) { + x = w - x; + y = h - y; + } else if (orientation == 270) { + double temp = x; + x = y; + y = h - temp; + } + return new double[]{x, y}; + } + + /** + * Convert the coordinate system of poco to the coordinates in the adb vertical coordinate system + * + * @param x x vertical coordinate x + * @param y y vertical coordinate y + * @param w w ScreenWidth + * @param h h ScreenHeight + * @param orientation The device orientation, based on the vertical direction of the mobile device, + * rotate 90° counterclockwise, orientation=90, 180° counterclockwise, orientation=180, + * and so on + * @return {@link int[]} + */ + public static double[] VerticalTransformerPoco(double x, double y, double w, double h, Integer orientation) { + if (orientation == 90) { + double temp = x; + x = y; + y = w - temp; + } else if (orientation == 180) { + x = w - x; + y = h - y; + } else if (orientation == 270) { + double temp = x; + x = h - y; + y = temp; + } + return new double[]{x, y}; + } +} diff --git a/src/test/java/org/cloud/sonic/driver/common/tool/PocoXYTransformerTest.java b/src/test/java/org/cloud/sonic/driver/common/tool/PocoXYTransformerTest.java new file mode 100644 index 0000000..c7be21e --- /dev/null +++ b/src/test/java/org/cloud/sonic/driver/common/tool/PocoXYTransformerTest.java @@ -0,0 +1,42 @@ +package org.cloud.sonic.driver.common.tool; + +import org.junit.Assert; +import org.junit.Test; + +import javax.swing.plaf.synth.SynthOptionPaneUI; + +public class PocoXYTransformerTest { + double width = 100, height = 1000; + + @Test + public void testOriToUP() { + double pocoX = 100, pocoY = 50; + double[]result = PocoXYTransformer.PocoTransformerVertical(pocoX,pocoY,width,height,270); + Assert.assertEquals(result[0],pocoY,0); + Assert.assertEquals(result[1],height-pocoX,0); + System.out.printf("x:%s,y:%s",result[0],result[1]); + System.out.println(); + + result = PocoXYTransformer.PocoTransformerVertical(pocoX,pocoY,width,height,90); + Assert.assertEquals(result[0],pocoY,0); + Assert.assertEquals(result[1],pocoX,0); + System.out.printf("x:%s,y:%s",result[0],result[1]); + System.out.println(); + } + + @Test + public void testUPToOri() { + double upx = 50,upy = 100; + double[]result = PocoXYTransformer.VerticalTransformerPoco(upx,upy,width,height,270); + Assert.assertEquals(result[0],height-upy,0); + Assert.assertEquals(result[1],upx,0); + System.out.printf("x:%s,y:%s",result[0],result[1]); + System.out.println(); + + result = PocoXYTransformer.VerticalTransformerPoco(upx,upy,width,height,90); + Assert.assertEquals(result[0],upy,0); + Assert.assertEquals(result[1],upx,0); + System.out.printf("x:%s,y:%s",result[0],result[1]); + System.out.println(); + } +}