Scolring - Forum

Entraides et échanges autour de la technologie Scol - Informations and exchanges on the Scol technology

Vous pouvez changer la langue de l'interface une fois inscrit - You can change the language once registered

You are not logged in.

#1 26-Sep-2024 19:13:36

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,524
Website

hexagon grid project wip

i figure before I mess with hexagons in 3d in os3d, I will make a hexagon grid painted into a bitmap. then i will use some of the math to position hexagon shaped meshes in 3d space.

struct XYVertex=[
	posx:	I,
	posy: I
	]mkXYVertex;;//this makes a vertex object with xy coordinates
	
struct QRSCoordinate=[
	posq:	I,
	posr: I,
	poss: I
	]mkQRSCoordinate;;//this makes a vertex object with qrs coordinates. It is easier to create a grid
//of hexagons this way and you can use xyz vector functions with your hexagons
	
	struct Hexagon=[
	VQRScenter:	QRSCoordinate,
	Isize:	I,
	//table of edge points in xy coordinate system with XYVertex objects, since we always know that there are only 6 such points we can use a table
	lxypoints:	tab [XYVertex],
	//table  of edge points in qrs (xyz) coordinate system with QRSCoordinate objects,since we always know that there are only 6 such points we can use a table
	lqrspoints: tab [QRSCoordinate],
	//send vertex and hexagon to function from a mouse click, get whether in outside, 0, or inbounds, 1
	//I probably won't need this when i make it in 3d and I may not work on this at all
	funcwithinbounds: fun [[Hexagon XYVertex]] I,
	//send hexagon to function with center and size, create vertex coordinates with some geometry math
	//so as to load return into lpoints table
	funmakevertices: fun [Hexagon] [tab [XYVertex] ],
	//send hexagon to function with xy coordinates, get back hexagon 
	//with qrs (xyz) coordinates as they are easier to work with and to store center of hex
	//in QRSCoordinate object
	funconvert_xy_to_qrs: fun [Hexagon][I I I],	
	//reverse  of funconvert_xy_to_qr so we can set hexagon center (and maybe edge points?) in xy space
	funconvert_qrs_to_xy:	fun [Hexagon][I I]


	
	]mkHexagon;;

   //this object will hold a bunch of hexagons in a grid in qrs coordinate system
   //this will be very important to build a hexagon grid with hexagon .mesh object in os3d i think
	struct Hexagongrid=[
		hexagongrid: [Hexagon r1]
			] mkHexagongrid;;
	
fun main()=
	_showconsole;
	0;;

so far so good it runs, but I haven't actually done anything yet. neutral neutral neutral



my first step will be to draw one humble hexagon into a bitmap using the information gained from a Hexagon object and the _DRAWpoly24 function.
then to paint a grid of hexagons into a bitmap (this is intimidating as you position the hexagons in qrs space and convert everything to xy space so you can put them in a bitmap that only understands xy not qrs.)

Last edited by hebdemnobad (26-Sep-2024 19:27:37)

Offline

#2 6-Oct-2024 05:31:25

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,524
Website

Re: hexagon grid project wip

ok. i think i can create an array of hex objects in qrs space that is an arbitrary height in rows of hexes and width in columns of hexes, the size of each hex, and whether the first column is lower than the second colum or the first column is higher than the second column, where the q r and s coordinates always add to zero.

the problem is turning the qrs (like xyz coordinates) coordinates into xy coordinates so they can be drawn or be used to arrange meshes in os3d.  fortunately i have a retired math professor in the family and got him interested enough in the qrs conversion to xy conversion... (the code below has preloaded the consle.pkg from the tutorials to display everything.

hextable.scol

_load "hextable/hexload.pkg"

hexload.pkg

proto hextablemain = fun [] I;;
proto consolemain = fun[] I;;



fun main()=
_showconsole; 
_load "os3dlib/tools.pkg";
_load "hextable/console.pkg";
_load "hextable/hextable.pkg";


_fooS "in hexload main function";
consolemain;
hextablemain;
0;;

hextable.pkg

// nested table syntax typeof Globalgrid = tab [I tab [S tab F]];;



typeof Global_startvectable = tab QRSCoordinate;;
typeof Global_hextable = tab tab Hex;;//this is a table. each element in the table contains a table of hex objects. the table is total_columns long and total  rows high see makehexgrid function below
proto mygetcoordinates = fun [QRSCoordinate] [I I I];;
proto makestartvectable = fun [I I S] I;;
proto scalevector = fun [[I I I] I ] [I I I];;

//vertex object with qrs coordinates
struct QRSCoordinate=[
	posq:	I,
	posr: I,
	poss: I,
	getcoordinates: fun [QRSCoordinate] [I I I]
	]mkQRSCoordinate;;
	
struct Hex= [
	hexcenter: 	QRSCoordinate,
	hexsize:		I,
	fungethexcenter: fun [Hex]  [I I I],
	funprinthex: fun [Hex] S
	]mkHex;;
	
	
	
fun get_full_startvector_table_contents (startvectortable)= 	

fun mygetcoordinates(qrs)=
	let qrs.posq-> q in
	let qrs.posr-> r in
	let qrs.poss-> s in
	[q r s];;
	
fun getmyhexcoordinates (hex) =
	let hex.hexcenter -> qrs_center in
	mygetcoordinates qrs_center;; //this function takes hex and returns [q r s] for qrs object in its center

fun myprinthex (hex)=
	let exec hex.fungethexcenter with [hex] -> [q r s] in
	let strcatn "this hex q center is at: ":: (itoa q)::" it's r center is at: ":: (itoa r):: " and its s center is at: "::(itoa s)::nil -> string in
	(
		console_print lastcsl string;
		string);;
	
fun makeQRS(q, r, s)=
	let mkQRSCoordinate [q r s nil]-> qrs in
	(
	set qrs.getcoordinates=@mygetcoordinates;
	qrs);;
	
fun makeHex(qrs, size)=
let mkHex [qrs size  @getmyhexcoordinates @myprinthex] -> hex in
hex;;

fun scalevector (directional_vector, scale_factor)=
	let directional_vector -> [q r s] in
	let q*scale_factor -> newq in
	let r*scale_factor -> newr in
	let s*scale_factor -> news in
	[newq newr news];;


fun addvec_with_direction (inputvector, direction, distance)= //this function takes a vector and direction and returns a new vector object,the result depending on the the direction this package has already loaded the os3d tools.pkg so it can use functions from it
	
	if ((strcmp direction "northeast") ==0) then
	(
	//console_print lastcsl"adding new vector to the northeast!";
	let [1 (-1) 0] -> directional_vector in
	let distance -> factor in
	let scalevector directional_vector factor ->  scaled_directional_vector in
	let addVector inputvector scaled_directional_vector -> outputvector in

	let outputvector -> [q r s] in
	(
	//console_print lastcsl strcatn "new q is: "::(itoa q)::" new r is: "::(itoa r):: " and new s is: ":: (itoa s)::nil;
	outputvector;
	);
	)
	else if ((strcmp direction "southeast") ==0) then
	(
	let [1 0 (-1)] -> directional_vector in
	let distance -> factor in
	let scalevector directional_vector factor ->  scaled_directional_vector in
	let addVector inputvector scaled_directional_vector -> outputvector in
	
	let outputvector -> [q r s] in
	(
	//console_print lastcsl strcatn "new q is: "::(itoa q)::" new r is: "::(itoa r):: " and new s is: ":: (itoa s)::nil;
	outputvector;
	);
	)
	
	else if ((strcmp direction "north") ==0) then
	(
	//console_print lastcsl"adding new vector to the north!";
	let [0 (-1) 1] -> directional_vector in
	let distance -> factor in
	let scalevector directional_vector factor ->  scaled_directional_vector in
	let addVector inputvector scaled_directional_vector -> outputvector in
	
	let outputvector -> [q r s] in
	(
	//console_print lastcsl strcatn "new q is: "::(itoa q)::" new r is: "::(itoa r):: " and new s is: ":: (itoa s)::nil;
	let q+r+s -> result in
	if (result == 0) then console_print lastcsl"no error" else console_print lastcsl"error!!";
	outputvector;
	);
	)
	
	
	else
	(
	nil
	);;




fun make_column (hexgrid, hexlist, currentcolumn_number, currentrow_number, inputqrs, total_rows, hexsize)=
console_print lastcsl strcatn "presently in column number: ":: (itoa currentcolumn_number)::nil;
if (currentrow_number<total_rows)  then
(

console_print lastcsl strcatn "currently in row number: ":: (itoa currentrow_number):: " and we will take the inputqrs and make a hex here!":: "\n":: "then we will feed a new QRS object into the function"::nil;
let makeHex inputqrs hexsize-> hex in
let lcat hexlist hex::nil -> hexlist in
let exec inputqrs.getcoordinates with [inputqrs] -> inputvec in
let addvec_with_direction inputvec "north" hexsize -> newvec in
let newvec -> [q r s] in
let makeQRS q r s -> newqrs in
(
 myprinthex hex;
set currentrow_number=currentrow_number+1;
make_column hexgrid hexlist currentcolumn_number currentrow_number newqrs total_rows hexsize;
hexlist;
);
)

else //we are done with the column, turn the list into a table, and put it in the currentcolumn_number of the Global_hextable
(
console_print lastcsl strcatn "done with column number: " ::(itoa currentcolumn_number):: nil;
let sizelist hexlist -> size in
let listtotab hexlist -> hextable in
let sizetab hextable -> tablesize in
(
console_print lastcsl strcatn "there are : ":: (itoa size):: " items inthe hexlist"::nil;
console_print lastcsl strcatn "there are : ":: (itoa tablesize):: " items inthe hextable"::nil;
set Global_hextable.currentcolumn_number = hextable;
let sizetab Global_hextable.currentcolumn_number -> sizable2 in
console_print lastcsl strcatn "there are :" :: (itoa sizable2):: " elements in column number"::(itoa currentcolumn_number)::nil;
hexlist;
//todo test contents of table of tables of hexes and validate their  coordintaes
);
);;
	



	
	
fun fillvectortable (vector, hexsize, qrstable, startstate, rownumber)=
	if (rownumber ==0) then
	(
	console_print lastcsl "we are at start vecdtor table item zero;Global_startvectable.0  has already been set, the only thing left it to determine whether to go southeast (evens up) or northeast (odds up)";
	if ((strcmp startstate "evens up") ==0) then 
	(
		//go southeast, and feed in vector of this item to form next item
		//we want to make vector for first odd number to feed into function
		let addvec_with_direction  vector "southeast" hexsize-> newvector in
		(
		 set rownumber = rownumber+1;
	    fillvectortable newvector hexsize qrstable startstate rownumber;
		0;
		);
		
	0;
	) else
	(
		let addvec_with_direction  vector "northeast" hexsize-> newvector in
		(
		 set rownumber = rownumber+1;
	    fillvectortable newvector hexsize qrstable startstate rownumber;
		0;
		);
		
		//go northeast and feed in vector of this item to form next item
		//we want to make vector for first odd number to feed into function
		
	0;
	);
		
	0;
	)
	
	
	
	else if (rownumber == (sizetab qrstable)) then
	(
	console_print lastcsl"we are at end of table; ";
	get_full_startvector_table_contents Global_startvectable;
	0;
	)
	else
	(
	if ((mod rownumber  2)!= 0) then//odd number fork 
	(
			console_print lastcsl"we have an odd  number";
			if ((strcmp startstate "evens up") ==0) then //if evens up then odd numbers make a new vector to the northeast
			(
					console_print lastcsl"go northeast because evens are up and odds are down";
					let addvec_with_direction  vector "northeast" hexsize-> newvector in
					let vector -> [q r s] in
					let makeQRS q r s -> newqrs in
					(
               set Global_startvectable.rownumber = newqrs;
               set rownumber= rownumber+1;
               fillvectortable newvector hexsize Global_startvectable startstate rownumber;
					);
			0;
			)
			else
			(
					console_print lastcsl"go southeast because odds are above evens"; //if evens down then odd numbers make a new vector to the southeast
					let addvec_with_direction  vector "southeast" hexsize -> newvector in
					let vector -> [q r s] in
					let makeQRS q r s -> newqrs in
					(
               set Global_startvectable.rownumber = newqrs;
               set rownumber= rownumber+1;
               fillvectortable newvector hexsize Global_startvectable startstate rownumber;
					);
			0;
			); 
	0;
   )
   
   ///even number fork
  else
   (
   console_print lastcsl"we have an even number";
   if ((strcmp startstate "evens up") ==0) then
			(
					console_print lastcsl"go southeast because evens are up and odds are down";//if evens up then even numbers make a new vector to the southheast
					let addvec_with_direction  vector "southeast" hexsize-> newvector in
					let vector -> [q r s] in
					let makeQRS q r s -> newqrs in
					(
               set Global_startvectable.rownumber = newqrs;
               set rownumber= rownumber+1;
               fillvectortable newvector hexsize Global_startvectable startstate rownumber;
					);
			0;
			)
			else
			(
					console_print lastcsl"go northeast because odds are above evens"; //if odds up then even numbers make a new vector to the norththheast
					let addvec_with_direction  vector "northeast" hexsize-> newvector in
					let vector -> [q r s] in
					let makeQRS q r s -> newqrs in
			
					(
               set Global_startvectable.rownumber = newqrs;
               set rownumber= rownumber+1;
               fillvectortable newvector hexsize Global_startvectable startstate rownumber;
					);
			0;
			); 
	0;
   );
   0;
 	);
  	0;;


	
fun makestartvectable(size, hexsize, startstate)=
	set Global_startvectable = mktab size nil;
	let makeQRS 0 0 0 -> r in
	let 0 -> rownumber in
	let mygetcoordinates r -> startcoordiantes in//this will be [0 0 0]
	(
	set Global_startvectable.0 = r;
	fillvectortable  startcoordiantes hexsize Global_startvectable startstate rownumber;
	0;
	);
	0;;
	
	fun makehexgrid (total_columns, total_rows, hexsize, startstate)=
	makestartvectable total_columns hexsize startstate;
	let mktab total_columns nil-> table in
	let 0-> column_counter in
	let 0-> column_number in
	(
	set Global_hextable= table;
	while (column_counter < total_columns) do
	(
	let Global_startvectable.column_counter -> initialqrs in
	//make_column arguments: hexgrid, hexlist, currentcolumn_number, currentrow_number, inputvec, total_row, hex size
  make_column table nil column_counter 0 initialqrs total_rows hexsize;
  set column_counter= column_counter +1;
  );
  0;
  );
	0;;

fun hextablemain()=
_showconsole;
//the number after the function call is the number of vectors in the vector table which will be the number of columns in the hex table

makehexgrid  3 3 1 "evens up";
//do some tests
myprinthex Global_hextable.0.0; 
myprinthex Global_hextable.1.2; 
myprinthex Global_hextable.2.2;
0;;

Last edited by hebdemnobad (6-Oct-2024 17:37:00)

Offline

#3 8-Oct-2024 02:19:37

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,524
Website

Re: hexagon grid project wip

i've gotten it all working (in text, next step to draw these things...i know for os3d you don't have to draw hexagons but will use meshes but I wanted to make sure that the hexagons all fit together before I build the plugit. the goal here is to make 2d maps for table top games to keep track of where the players (and monsters) are, without having to look through rulebooks all the time.


the code builds a rectangular array of hexagons in q r s (like xyz) space, and then when needed translates the qrs coordinates into xy coordinates to draw in 2d eventually. the code can create rectangular arrays of arbitrary size, with hexagons of arbitrary size with arbitrary starting qrs coordinates (although the coordinates have to make sense in qrs space; if they don't, all the subsequent calculations will be wrong.)


note: y is calculated with a -1 so it makes sense like a mathematical graph where y is 0 at the bottom left. since with drawing apps including scol you start drawing at the top left, remove the -1.

typeof Global_startvectable = tab QRSCoordinate;;
typeof Global_hextable = tab tab Hex;;//this is a table. each element in the table contains a table of hex objects. the table is total_columns long and total  rows high see makehexgrid function below
proto mygetcoordinates = fun [QRSCoordinate] [I I I];;
proto makestartvectable = fun [I I S [I I I]] I;; // columns, rows, startstate, start vector location
proto scalevector = fun [[I I I] I ] [I I I];;
proto hex_to_pixel = fun [Hex] [I I];;

//vertex object with qrs coordinates
struct QRSCoordinate=[
	posq:	I,
	posr: I,
	poss: I,
	getcoordinates: fun [QRSCoordinate] [I I I]
	]mkQRSCoordinate;;
	
struct Hex= [
	hexcenter: 	QRSCoordinate,
	hexheight:		I,
	fungethexcenter: fun [Hex]  [I I I],
	funprinthex: fun [Hex] S,
	funhextopixel: fun [Hex] [I I],
	hex_xy: [I I]
	]mkHex;;
	
	
	
fun get_full_startvector_table_contents (startvectortable)= 
	let sizetab startvectortable ->table_total in
	let 0-> counter in
	(
	while (counter< table_total) do
	(
	
	let Global_startvectable.counter-> qrsobject in
	let  mygetcoordinates qrsobject -> [q r s] in
	(
	console_print lastcsl strcatn "present q r s coordinates in start vector table are: ":: (itoa q)::"  "::(itoa r):: "  ":: (itoa s)::nil;
	0;
	);
	set counter = counter+1;
	);
	console_print lastcsl strcatn "at end of table, finally!  the table had a total of: ":: (itoa table_total)::" items in it"::nil;
	);
			0;;
	

fun mygetcoordinates(qrs)=
	let qrs.posq-> q in
	let qrs.posr-> r in
	let qrs.poss-> s in
	[q r s];;
	
fun getmyhexcoordinates (hex) =
	let hex.hexcenter -> qrs_center in
	mygetcoordinates qrs_center;; //this function takes hex and returns [q r s] for qrs object in its center

fun myprinthex (hex)=
	let exec hex.fungethexcenter with [hex] -> [q r s] in
	let hex.	hex_xy -> [x y] in
	let strcatn "this hex q center is at: ":: (itoa q)::" it's r center is at: ":: (itoa r):: " and its s center is at: "::(itoa s)::"\n"::" the converted hex x coordinate is: ":: (itoa x)::" and the converted hex y coordinate is:"  :: (itoa y)::nil -> string in
	(
		console_print lastcsl string;
		string);;

fun myhextopixel(hex)=
	let getmyhexcoordinates hex -> [q r s] in

	if ( q == 0) then 
	(	
	nil;
	)
	 else
	(
   let _fooS strcatn "calculating hextopixel; hex qrs is: ":: (itoa q)::" "::(itoa r)::" "::(itoa s):: nil -> string in
	let hex.hexheight -> height in
	let (itof height) -> float_height in
	let _fooS strcatn "the float height is: ":: (ftoa float_height)::" : this is half the full height of a flat top hexagon"::nil -> string in
	let ((float_height *. 2.0) *. sqrt (3.0)) /. 3.0 -> float_final_size in
	let _fooS strcatn "the final size of the hex, in terms of horizontal distande to any point, is"::  (ftoa float_final_size):: nil -> string in
	let (float_final_size *. (3.0 /. 2.0) *. (itof q) ) -> xfloat in
	let xfloat *. (itof hex.hexheight) -> xfloat in
	let _fooS strcat "the x value is" (ftoa xfloat) -> string in
   let (sqrt (3.0) /. (2.0)) *.  (itof q) -> addendum1 in
	let sqrt (3.0) *. (itof r) -> addendum2 in
	let _fooS strcat "float r is" (itoa r) -> string in
	let float_final_size *.  (sqrt 3.0) *. (itof q) /. (2.0) -> test_addenda1 in
	let float_final_size *. (sqrt 3.0) *. (itof r) -> test_addenda2 in
	let test_addenda1 +. test_addenda2 -> sum in 
	let sum *. 1.0-> sum in //change to - (1.0) if drawing from origin at bottom left but most screens start at top left
	let _fooS strcat "final calculated y value is" (ftoa sum) -> string in
	[1 1]);;
	
	
	
fun myhextopixel2 (hexheight, q, r)=
	
	let hexheight -> height in
	let (itof height) -> float_height in
	let ((float_height *. 2.0) *. sqrt (3.0)) /. 3.0 -> float_final_size in
	let (float_final_size *. 3.0) /. ((2.0 *. (itof q) )) -> xfloat in
	let xfloat *. (itof hexheight) -> xfloat in
	let _fooS strcat "the x value is" (ftoa xfloat) -> string in
   let (sqrt (3.0) /. (2.0)) *.  (itof q) -> addendum1 in
	let sqrt (3.0) *. (itof r) -> addendum2 in
	let _fooS strcat "float r is" (itoa r) -> string in
	let float_final_size *.  (sqrt 3.0) *. (itof q) /. (2.0) -> test_addenda1 in
	let float_final_size *. (sqrt 3.0) *. (itof r) -> test_addenda2 in
	let test_addenda1 +. test_addenda2 -> sum in 
	let sum *. (-.1.0)-> yfloat in
	let (ftoi xfloat) -> x in
	let (ftoi yfloat) -> y in
	[x y];;
	
	
fun makeQRS(q, r, s)=
	let mkQRSCoordinate [q r s nil]-> qrs in
	(
	set qrs.getcoordinates=@mygetcoordinates;
	qrs);;
	
fun makeHex(qrs, size)=
let mkHex [qrs size  @getmyhexcoordinates @myprinthex @myhextopixel nil ] -> hex in
let exec hex.funhextopixel with [hex] -> [x y] in
(
set hex.hex_xy = [x y];
hex);;

fun scalevector (directional_vector, scale_factor)=
	let directional_vector -> [q r s] in
	let q*scale_factor -> newq in
	let r*scale_factor -> newr in
	let s*scale_factor -> news in
	[newq newr news];;


fun addvec_with_direction (inputvector, direction, distance)= //this function takes a vector and direction and returns a new vector object,the result depending on the the direction this package has already loaded the os3d tools.pkg so it can use functions from it
	
	if ((strcmp direction "northeast") ==0) then
	(
	//console_print lastcsl"adding new vector to the northeast!";
	let [1 (-1) 0] -> directional_vector in
	let distance -> factor in
	let scalevector directional_vector factor ->  scaled_directional_vector in
	let addVector inputvector scaled_directional_vector -> outputvector in

	let outputvector -> [q r s] in
	(
	//console_print lastcsl strcatn "new q is: "::(itoa q)::" new r is: "::(itoa r):: " and new s is: ":: (itoa s)::nil;
	outputvector;
	);
	)
	else if ((strcmp direction "southeast") ==0) then
	(
	let [1 0 (-1)] -> directional_vector in
	let distance -> factor in
	let scalevector directional_vector factor ->  scaled_directional_vector in
	let addVector inputvector scaled_directional_vector -> outputvector in
	
	let outputvector -> [q r s] in
	(
	//console_print lastcsl strcatn "new q is: "::(itoa q)::" new r is: "::(itoa r):: " and new s is: ":: (itoa s)::nil;
	outputvector;
	);
	)
	
	else if ((strcmp direction "north") ==0) then
	(
	//console_print lastcsl"adding new vector to the north!";
	let [0 (-1) 1] -> directional_vector in
	let distance -> factor in
	let scalevector directional_vector factor ->  scaled_directional_vector in
	let addVector inputvector scaled_directional_vector -> outputvector in
	
	let outputvector -> [q r s] in
	(
	//console_print lastcsl strcatn "new q is: "::(itoa q)::" new r is: "::(itoa r):: " and new s is: ":: (itoa s)::nil;
	let q+r+s -> result in
	if (result == 0) then console_print lastcsl"no error" else console_print lastcsl"error!!";
	outputvector;
	);
	)
	
	
	else
	(
	nil
	);;




fun make_column (hexgrid, hexlist, currentcolumn_number, currentrow_number, inputqrs, total_rows, hexsize)=
console_print lastcsl strcatn "presently in column number: ":: (itoa currentcolumn_number)::nil;
if (currentrow_number<total_rows)  then
(

console_print lastcsl strcatn "currently in row number: ":: (itoa currentrow_number):: " and we will take the inputqrs and make a hex here!":: "\n":: "then we will feed a new QRS object into the function"::nil;
let exec inputqrs.getcoordinates with [inputqrs] -> inputvec in
let inputvec -> [inputq inputr inputs] in
let _fooS strcatn "inputvec for hex in column number: ":: (itoa currentcolumn_number ):: "and row number: ":: (itoa currentrow_number):: "\n":: "is q: ":: (itoa inputq )::" , r: ":: (itoa inputr):: " , and s: "::(itoa inputs):: nil-> string in
let makeHex inputqrs hexsize-> hex in
let lcat hexlist hex::nil -> hexlist in
let exec inputqrs.getcoordinates with [inputqrs] -> inputvec in
let addvec_with_direction inputvec "north" hexsize -> newvec in
let newvec -> [q r s] in
let makeQRS q r s -> newqrs in
(
 myprinthex hex;
set currentrow_number=currentrow_number+1;
make_column hexgrid hexlist currentcolumn_number currentrow_number newqrs total_rows hexsize;
hexlist;
);
)

else //we are done with the column, turn the list into a table, and put it in the currentcolumn_number of the Global_hextable
(
console_print lastcsl strcatn "done with column number: " ::(itoa currentcolumn_number):: nil;
let sizelist hexlist -> size in
let listtotab hexlist -> hextable in
let sizetab hextable -> tablesize in
(
console_print lastcsl strcatn "there are : ":: (itoa size):: " items inthe hexlist"::nil;
console_print lastcsl strcatn "there are : ":: (itoa tablesize):: " items inthe hextable"::nil;
set Global_hextable.currentcolumn_number = hextable;
let sizetab Global_hextable.currentcolumn_number -> sizable2 in
console_print lastcsl strcatn "there are :" :: (itoa sizable2):: " elements in column number"::(itoa currentcolumn_number)::nil;
hexlist;
//todo test contents of table of tables of hexes and validate their  coordintaes
);
);;
	



	
	
fun fillvectortable (vector, hexsize, qrstable, startstate, rownumber)=
	if (rownumber ==0) then
	(
	console_print lastcsl "we are at start vecdtor table item zero;Global_startvectable.0  has already been set, the only thing left it to determine whether to go southeast (evens up) or northeast (odds up)";
	if ((strcmp startstate "evens up") ==0) then 
	(
		//go southeast, and feed in vector of this item to form next item
		//we want to make vector for first odd number to feed into function
		let addvec_with_direction  vector "southeast" hexsize-> newvector in
		(
		 set rownumber = rownumber+1;
	    fillvectortable newvector hexsize qrstable startstate rownumber;
		0;
		);
		
	0;
	) else
	(
		let addvec_with_direction  vector "northeast" hexsize-> newvector in
		(
		 set rownumber = rownumber+1;
	    fillvectortable newvector hexsize qrstable startstate rownumber;
		0;
		);
		
		//go northeast and feed in vector of this item to form next item
		//we want to make vector for first odd number to feed into function
		
	0;
	);
		
	0;
	)
	
	
	
	else if (rownumber == (sizetab qrstable)) then
	(
	console_print lastcsl"we are at end of table; ";
	get_full_startvector_table_contents Global_startvectable;
	0;
	)
	else
	(
	if ((mod rownumber  2)!= 0) then//odd number fork 
	(
			console_print lastcsl"we have an odd  number";
			if ((strcmp startstate "evens up") ==0) then //if evens up then odd numbers make a new vector to the northeast
			(
					console_print lastcsl"go northeast because evens are up and odds are down";
					let addvec_with_direction  vector "northeast" hexsize-> newvector in
					let vector -> [q r s] in
					let makeQRS q r s -> newqrs in
					(
               set Global_startvectable.rownumber = newqrs;
               set rownumber= rownumber+1;
               fillvectortable newvector hexsize Global_startvectable startstate rownumber;
					);
			0;
			)
			else
			(
					console_print lastcsl"go southeast because odds are above evens"; //if evens down then odd numbers make a new vector to the southeast
					let addvec_with_direction  vector "southeast" hexsize -> newvector in
					let vector -> [q r s] in
					let makeQRS q r s -> newqrs in
					(
               set Global_startvectable.rownumber = newqrs;
               set rownumber= rownumber+1;
               fillvectortable newvector hexsize Global_startvectable startstate rownumber;
					);
			0;
			); 
	0;
   )
   
   ///even number fork
  else
   (
   console_print lastcsl"we have an even number";
   if ((strcmp startstate "evens up") ==0) then
			(
					console_print lastcsl"go southeast because evens are up and odds are down";//if evens up then even numbers make a new vector to the southheast
					let addvec_with_direction  vector "southeast" hexsize-> newvector in
					let vector -> [q r s] in
					let makeQRS q r s -> newqrs in
					(
               set Global_startvectable.rownumber = newqrs;
               set rownumber= rownumber+1;
               fillvectortable newvector hexsize Global_startvectable startstate rownumber;
					);
			0;
			)
			else
			(
					console_print lastcsl"go northeast because odds are above evens"; //if odds up then even numbers make a new vector to the norththheast
					let addvec_with_direction  vector "northeast" hexsize-> newvector in
					let vector -> [q r s] in
					let makeQRS q r s -> newqrs in
			
					(
               set Global_startvectable.rownumber = newqrs;
               set rownumber= rownumber+1;
               fillvectortable newvector hexsize Global_startvectable startstate rownumber;
					);
			0;
			); 
	0;
   );
   0;
 	);
  	0;;


	
fun makestartvectable(size, hexsize, startstate, startvec) =
	set Global_startvectable = mktab size nil;
	let startvec -> [q r s] in
	let makeQRS q r s -> r in
	let 0 -> rownumber in
	let mygetcoordinates r -> startcoordiantes in//this will be [0 0 0]
	(
	set Global_startvectable.0 = r;
	fillvectortable  startcoordiantes hexsize Global_startvectable startstate rownumber;
	0;
	);
	0;;
	
fun makehexgrid (total_columns, total_rows, hexsize, startstate, startvec)=
	makestartvectable total_columns hexsize startstate startvec;
	let mktab total_columns nil-> table in
	let 0-> column_counter in
	let 0-> column_number in
	(
	set Global_hextable= table;
	while (column_counter < total_columns) do
	(
	let Global_startvectable.column_counter -> initialqrs in
	//make_column arguments: hexgrid, hexlist, currentcolumn_number, currentrow_number, inputvec, total_row, hex size
  make_column table nil column_counter 0 initialqrs total_rows hexsize;
  set column_counter= column_counter +1;
  );
  0;
  );
	0;;

fun hextablemain()=
_showconsole;
//the number after the function call is the number of vectors in the vector table which will be the number of columns in the hex table

makehexgrid  30 50 1 "evens up" [1 3 (-4)]; // columns, rows, hexsize (vertical height for flat top), start state evens up or odds up
_fooS "the start state is evens up!";
//do some tests
_fooS "testing";
_fooS myprinthex Global_hextable.1.0; 
_fooS "hextopixel test";
exec Global_hextable.0.0.funhextopixel with [ Global_hextable.0.0] ;
exec Global_hextable.0.1.funhextopixel with [ Global_hextable.0.1] ;
exec Global_hextable.0.2.funhextopixel with [ Global_hextable.0.2] ;
exec Global_hextable.0.3.funhextopixel with [ Global_hextable.0.3] ;
exec Global_hextable.0.4.funhextopixel with [ Global_hextable.0.4] ;
exec Global_hextable.0.5.funhextopixel with [ Global_hextable.0.5] ;

exec Global_hextable.1.0.funhextopixel with [ Global_hextable.1.0] ;
exec Global_hextable.1.1.funhextopixel with [ Global_hextable.1.1] ;
exec Global_hextable.1.2.funhextopixel with [ Global_hextable.1.2] ;
exec Global_hextable.1.3.funhextopixel with [ Global_hextable.1.3] ;
exec Global_hextable.1.4.funhextopixel with [ Global_hextable.1.4] ;
exec Global_hextable.1.5.funhextopixel with [ Global_hextable.1.5] ;

exec Global_hextable.2.0.funhextopixel with [ Global_hextable.2.0] ;
exec Global_hextable.2.1.funhextopixel with [ Global_hextable.2.1] ;
exec Global_hextable.2.2.funhextopixel with [ Global_hextable.2.2] ;
exec Global_hextable.2.3.funhextopixel with [ Global_hextable.2.3] ;
exec Global_hextable.2.4.funhextopixel with [ Global_hextable.2.4] ;
exec Global_hextable.2.5.funhextopixel with [ Global_hextable.2.5] ;


0;;

Last edited by hebdemnobad (8-Oct-2024 16:24:09)

Offline

#4 14-Oct-2024 01:25:49

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,524
Website

Re: hexagon grid project wip

on the drawing end, I'm able to get the voyager to draw a hexagon in xy space on a bitmap, a small step, but since I can do that, after cleaning things up I'll try to draw a whole grid of them using the weird qrs coordinates used above



typeof drawwindow = ObjWin;;
typeof drawbitmap = ObjBitmap;;
typeof shapetable = tab tab Shape;; // a table of elements, and each elementt contains a one dimensional table of shapes
proto test_point_table  = fun [Mypolygon] I;;

struct Shape =[
	shapesides: I, //number of sides for each chape
	shaptepoints: tab [I I]
		] mkShape;;




struct Point =[
	x:	F,
	y:	F,
	fungetvalues: fun [Point] [F F] ///function to return x and y properties of Point
	] mkPoint;;
	

struct Mypolygon =[
numsides:			I,
center:				[F F], 	//center of polygon
sidelength:			F,
height:				F,
points:				tab Point, //table of point objects in polygon
funmakepoints:		fun [Mypolygon] tab  Point,
drawpointstable:	tab [I I], //function to create table of points to put in points table
hextype :			S,//for hexes, if "flat" then flat top, if "pointy" then pointy top
parentebitmap:		Objbitmap,
hexheight:			F //hexheight is vertical radius if flat and norizontal radius if pointy
	
	] mkMypolygon;;
	
	
fun paint (win, bitmap)=
		
		_PAINTwindow drawwindow;
		_BLTbitmap  drawwindow drawbitmap 0 0;
		
	
	0;;
	
fun end (window, parameter)=
	_DSbitmap  drawbitmap;
	_DSwindow  drawwindow;
	_closemachine;
	0;;

fun createinterface ()=
	_fooS "in createinterface function";
	set drawwindow = _CRwindow _channel nil 0 0 512 512 WN_NORMAL "image window"; 
	set drawbitmap = _CRbitmap _channel 512 512;
	_FILLbitmap drawbitmap 0xFFFFFF;
	_BLTbitmap  drawwindow drawbitmap 0 0;
	_CBwinDestroy drawwindow @end nil;
	_CBwinPaint  drawwindow @paint drawwindow;
	0;;

fun myfungetvalues(point)=
	let point.x -> x in
	let point.y -> y in
	[x y];;

fun initpoint (x, y)=
	let mkPoint [x y @myfungetvalues] -> point in
	point;;
	
fun makeflattophexpoints (polygon)=
	let ((polygon.hexheight *. 2.0) *. (sqrt 3.0)) /. 3.0 -> float_final_size in
	let _fooS strcat "at first generation, float_final_size  is" (ftoa float_final_size ) -> string in
   (
   let 0 -> counter in
	let polygon.center -> [functionx functiony] in
   while (counter < (sizetab polygon.points)) do
   (
   if (counter == 0) then
   (
	let 0.0 -> radians in
	let functionx +. (float_final_size *. (cos radians)) -> xpoint in
	let functiony +. (float_final_size *. (sin radians)) -> ypoint in
	let initpoint xpoint ypoint -> point in
	set polygon.points.counter = point;
	set counter = counter+1;
   )
   else
   (
   
   let 60.0 *. (itof counter) -> angle_degrees in
   let (PIf /. 180.0) *. angle_degrees -> radians in
   let functionx +. (float_final_size *. (cos radians)) -> xpoint in
	let functiony +. (float_final_size *. (sin radians)) -> ypoint in
	let initpoint xpoint ypoint -> point in
	set polygon.points.counter = point;
	set counter = counter+1;
	
   );
   );
   );
   0;;

fun hexmakepoints (polygon)=	
fun trianglemakepoints (polygon) = //this is only good for a triangle//return completed table of the three points in the triangle



fun test_point_table (polygon)=
	let polygon.points -> pointtable in
	let sizetab pointtable -> size in
	let 0 -> counter in
	while (counter < size) do
	(
 	let polygon.points.counter -> point in
	let exec point.fungetvalues with [point]-> [x y] in
	let _fooS strcatn "this is point number: ":: (itoa counter)::"\n":: "this point x is":: (ftoa x)::" and this point y is: ":: (ftoa y):: nil -> string in
	set counter = counter + 1;
	);
	0;;
	
fun test_polygon_drawpoints (polygon)=
	let sizetab polygon.drawpointstable -> size in
	let 0 -> counter in
	while (counter < size) do
	(
	let polygon.drawpointstable.counter -> [x y] in
	let _fooS strcatn "this is drawpoint number: ":: (itoa counter):: " its interger x is: ":: (itoa x):: " and its interger y is: ":: (itoa y):: nil -> string in
	set counter = counter +1;
	);
	polygon;;
	
fun draw_polygon_points (polygon)=
		
	_fooS "in draw_polygon_points function";
let 0 -> counter in
while (counter < (sizetab polygon.drawpointstable)) do
(
let polygon.points.counter -> point in
let exec point.fungetvalues with [point]-> [x y] in 
let _fooS strcatn "drawpoint number: ":: (itoa counter):: " has an x of: ":: (ftoa x):: " and a y of: " :: (ftoa y):: nil -> string in
set polygon.drawpointstable.counter  = [ (ftoi x) (ftoi y)];

set counter = counter +1;
0;
);
//test_polygon_drawpoints polygon;
let sizetab polygon.drawpointstable -> size in
let _fooS strcat "size of drawpointstable is: " (itoa size) -> string in
//the shape is done, along with it's drawpoints now we can create interface
createinterface;



let _fooS "about to blit bitmap!" -> string in
set drawbitmap = _CRbitmap _channel 512 512;
_FILLbitmap drawbitmap 0xFFFFFF;
let _DRAWpoly24 drawbitmap  (sizetab polygon.drawpointstable) polygon.drawpointstable DRAW_SOLID 1 0x000000 DRAW_INVISIBLE 0x99FFFF -> bmp in
_BLTbitmap  drawwindow drawbitmap 0 0; 
_PAINTwindow drawwindow;
paint drawwindow drawbitmap;




0;;

fun initpolygon (numsides, center, sidelength, hextype, hexheight ) =
let center -> [centerx centery] in
let mktab numsides nil -> pointstable in
let mktab numsides nil -> drawpointstable in //initialize empty points table to put in polygon.points property WARNING: YOU MUST CREAT EMPTY TABLE BEFORE YOU CAN START FILLING TABLE UP WITH ELEMENTS WITH VALUES!!!!
let mkMypolygon [numsides center sidelength nil pointstable nil drawpointstable hextype nil hexheight] -> polygon in
(
	if (numsides == 3) then // MAKE A TRIANGLE

	(
		let ((sqrt 3.0)/. 2.0) *. sidelength -> fheight in //CALCULATE HEIGHT OF TRIANGLE FROM SIDE LENGTH only works for TRIANGLES
		set polygon.height = fheight;
		set polygon.funmakepoints = @trianglemakepoints;
		trianglemakepoints polygon;
		polygon;
	) 
	else if (numsides == 6) then //MAKE A HEXAGON
	(
		
		
		
		
		set polygon.funmakepoints = @hexmakepoints;
	
		hexmakepoints polygon;
		polygon;
	)
	else
	
	(
	nil;
	);
	draw_polygon_points polygon;
	polygon;
	);;
	
fun main ()=
_showconsole;
///for a triangle, height is height from centroid to apex, for hex height is vertical radius tto flat top if flat top or to top point if pointytop
   let [256.0 256.0]-> center in
	test_point_table (initpolygon 6 center nil "flattop" 120.0);

    0;;

Offline

Board footer

Powered by FluxBB