Skip to content

Commit

Permalink
Update functions_for_transformation.js
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tedhyu authored Jul 3, 2024
1 parent 65b0b70 commit 44ca246
Showing 1 changed file with 97 additions and 14 deletions.
111 changes: 97 additions & 14 deletions functions_for_transformation.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<board.length;i++){
board_xy_x = transform[0][0]*board[i][0]+transform[1][0]*board[i][1]
board_xy_y = transform[0][1]*board[i][0]+transform[1][1]*board[i][1]
board_xy.push([board_xy_x,board_xy_y])
orig_x = x
orig_y = y
x = x - x_shift
y = y - y_shift

//create a list of possible hexagon the x and y can reside in.
lattice_y = (1/lattice_shift)*(inverse_transform[0][0]*x+inverse_transform[1][0]*y)
lattice_x = (1/lattice_shift)*(inverse_transform[0][1]*x+inverse_transform[1][1]*y)

//To find the lattice point closest, we have to consider four points and use distance formula to consider which is closest. Because there's decimals, we will consider four based on the fraction value. So for (8.5, 7.5), we will consider (8,7), (8,8), (9,7), and (9,8). There is probably more elegant mathematical way, but I haven't found any.
int_y = Math.floor(lattice_y)
int_x = Math.floor(lattice_x)
possible_lattice=[[int_x,int_y],[int_x,int_y+1],[int_x+1,int_y],[int_x+1,int_y+1]]

board_xy=[] //xy coordinate of possible lattice
for (i=0;i<possible_lattice.length;i++){
possible_lat_xy = hex_to_xy(possible_lattice[i][0],possible_lattice[i][1])
board_xy.push(possible_lat_xy)
}
console.log(board_xy)
return [1,2,3]

//find the distance between the mouse click and the lattice point closest to it. Keep the minimum index, so the correct lattice point is found for the hexagon the point lies in.
minimum = 1000000
min_index=0
for(i=0;i<board_xy.length;i++){
if(((board_xy[i][0]-orig_x)**2+(board_xy[i][1]-orig_y)**2)**(1/2)<minimum){
minimum = ((board_xy[i][0]-orig_x)**2+(board_xy[i][1]-orig_y)**2)**(1/2)
min_index=i
}
}


//Lastly: determine which part of the hexagon the point is in choices: (1-6).

dX = orig_x-board_xy[min_index][0]
dY = -(orig_y-board_xy[min_index][1]) //y axis is flipped from traditional y axis
arctan = Math.atan(dY/dX)

if(dX >= 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))

0 comments on commit 44ca246

Please sign in to comment.