From 44ca2463dd03bff8a983e0989904e79ebbd25329 Mon Sep 17 00:00:00 2001 From: Ted Yu Date: Wed, 3 Jul 2024 00:03:11 -0700 Subject: [PATCH] Update functions_for_transformation.js Finished the scripts. there are two main functions: hex_to_xy: converts a hexagon number and finds the x/y geometry coordinates. xy_to_hex This will convert the x y coordinates to which geometry the mouse is currently clicked over. --- functions_for_transformation.js | 111 ++++++++++++++++++++++++++++---- 1 file changed, 97 insertions(+), 14 deletions(-) diff --git a/functions_for_transformation.js b/functions_for_transformation.js index d1b1b4a..380e3c0 100644 --- a/functions_for_transformation.js +++ b/functions_for_transformation.js @@ -1,4 +1,28 @@ //This document will contain necessary function for the game. by tedhyu. +const lattice_shift = 3**(1/2) //multiplicative shift +const x_shift=-0.5 //x shift (needs to calibrate according to the board exactly with mouse) +const y_shift=(3**0.5)/2 //y shift (needs to calibrate according to the board exactly with mouse) + + //transformation matrix +transform = [[3**(1/2)/2,-1/2],[0,1]] +inverse_transform = [[2/3**(1/2),1/3**(1/2)],[0,1]] + + + +/*hex_to_xy + +input: the a and b lattice vector corresponding to the hexagon. (a,b) is designated for each hexagon. + +output: the x and y coordinates of hexagon (a,b). Note, the lattice_shift, x_shift, and y_shift correspionds to a specific grid that is shown in the image. This probably needs to be shifted for the actual program and adjust accordingly. +*/ +function hex_to_xy(a,b){ + lattice_x = (lattice_shift)*(transform[0][0]*b+transform[1][0]*a)+x_shift + lattice_y = (lattice_shift)*(transform[0][1]*b+transform[1][1]*a)+y_shift + return [lattice_x,lattice_y] +} + + + /*xy_to_hex @@ -14,20 +38,79 @@ With the value of a,b,c returned, the app can recognize which space the mouse cl For example, the (1,1) hexagon in the upper left corner. If the mouse clicks on its the lower right corner of this hexagon, the returned value is (1,1,3), which indicates that the right piece of the hexagon was clicked. Additional code willbe used to recognize that (1,1,3) corresponds to space #1 on the board. */ - function xy_to_hex(x,y){ - const lattice = 5 - transform = [[3**(1/2)/2,-1/2],[0,1]] //transformation matrix - - //board hexagon list - board=[[1,1],[1,2],[1,3],[1,4],[1,5],[1,6],[1,7],[1,8],[1,9],[2,1],[2,2],[2,3],[2,4],[2,5],[2,6],[2,7],[2,8],[2,9],[2,10],[3,2],[3,3],[3,4],[3,5],[3,6],[3,7],[3,8],[3,9],[3,10],[4,3],[4,4],[4,5],[4,6],[4,7],[4,8],[4,9],[4,10],[5,3],[5,4],[5,5],[5,6],[5,7],[5,8],[5,9],[5,10],[5,11],[6,3],[6,4],[6,5],[6,6],[6,7],[6,8],[6,9],[6,10],[6,11],[6,12],[7,4],[7,5],[7,6],[7,7],[7,8],[7,9],[7,10],[7,11],[7,12],[8,5],[8,6],[8,7],[8,8],[8,9],[8,10],[8,11],[8,12],[8,13],[9,5],[9,6],[9,7],[9,8],[9,9],[9,10],[9,11],[9,12],[9,13],[10,6],[10,7],[10,8],[10,9],[10,10],[10,11],[10,12],[10,13],[10,14],[11,6],[11,7],[11,8],[11,9],[11,10],[11,11],[11,12],[11,13],[11,14]] - board_xy=[] - //this changes the lattice number to xy coordinates - for (i=0;i= 0){ + if(arctan<=Math.PI/2 & arctan>=Math.PI/6){ + c = 2 + } + else if(arctan<=Math.PI/6 & arctan>=-Math.PI/6){ + c = 3 + } + else if(arctan<=-Math.PI/6 & arctan>=-Math.PI/2){ + c = 4 + } + } + else{ + if(arctan<=Math.PI/2 & arctan>=Math.PI/6){ + c = 5 + } + else if(arctan<=Math.PI/6 & arctan>=-Math.PI/6){ + c = 6 + } + else if(arctan<=-Math.PI/6 & arctan>=-Math.PI/2){ + c = 1 + } + } + return [possible_lattice[min_index][0],possible_lattice[min_index][1],c] + } + +//Example run: +//conversion of geometry index to x y coordinate. +//xy coordinate of center of hexagon (1,1). It should be at x = 1, y = sqrt(3) +console.log(hex_to_xy(1,1)) + + + + +//Example run2: +//Find the hexagon number and the section of the hexagon of coordinate, 7.9, 4*sqrt(3) +//as see in the image, it should be in section 3 of hexagon (6,5) +console.log(xy_to_hex(7.9,(3**0.5)*4))