Cant get the max value in an array?

4.2

Something is wrong while getting the max value in an array…

Iam merging 2 or 3 dictionary keys into an array, so far i have the array working, but when getting the max values in the array iam not sure what he is returning ?

print(“arrMax1_x: " + str(arrMax1_x) )
print(“arrMax1_y: " + str(arrMax1_y) )
print(”\narrMax:\n” + str(arrMax) )

arrMax1_x: 480
arrMax1_y: 192

arrMax:
[(32, 64), (32, 96), (32, 128), (32, 160), (32, 192), (64, 192), (96, 192), (128, 64), (160, 64), (160, 96), (160, 128), (160, 160), (160, 192), (-320, 64), (-320, 96), (-320, 128), (-320, 160), (-320, 192), (-288, 64), (-288, 96), (-288, 128), (-288, 160), (-288, 192), (-256, 64), (-256, 96), (-256, 128), (-256, 160), (-256, 192), (-224, 64), (-224, 96), (-224, 128), (-224, 160), (-224, 192), (-192, 64), (-192, 96), (-192, 128), (-192, 160), (-192, 192), (-160, 64), (-160, 96), (-160, 128), (-160, 160), (-160, 192), (-128, 64), (-128, 96), (-128, 128), (-128, 160), (-128, 192), (32, -224), (32, -192), (32, -160), (32, -128), (32, -96), (32, 352), (32, 384), ------------ (32, 416), (32, 448), (32, 480)---------, (64, -224), (64, -192), (64, -160), (64, -128), (64, -96), (64, 480), (96, -224), (96, -192), (96, -160), (96, -128), (96, -96), (96, 480), (128, -224), (128, -192), (128, -160), (128, -128), (128, -96), (160, -224), (160, -192), (160, -160), (160, -128), (160, -96), (160, 352), (160, 416), (160, 448), (160, 480), (192, -224), (192, -192), (192, -160), (192, -128), (192, -96), (192, 352), (192, 416), (192, 480), (224, -224), (224, -192), (224, -160), (224, -128), (224, -96), (224, 352), (224, 384), (224, 416), (224, 480), (288, 64), (288, 96), (288, 128), (288, 160), (288, 192), (320, 64), (320, 96), (320, 128), (320, 160), (320, 192), (352, 64), (352, 96), (352, 128), (352, 160), (352, 192), (384, 64), (384, 96), (384, 128), (384, 160), (384, 192), (416, 64), (416, 96), (416, 128), (416, 160), (416, 192), (448, 64), (448, 96), (448, 128), (448, 160), (448, 192), (480, 64), (480, 96), (480, 128), (480, 160), (480, 192)]

he returns 192 for arrMax1_y…
the values above have a bigger Y ----(32, 416), (32, 448), (32, 480)----

func btn_Mer_ly_pressed():

	var lays = itmListLayer.get_selected_items()
	if ( lays.size() <= 1 ):
		print( "tileDraw: only 1 layer selected"   )
		return;
	
	
	var max_imgPos = null
	arrMax.clear();
	
	for a1 in range(0, lays.size() ):
		var idx2 = lays[a1]
		var imgPos10 =  PlugTileDrawNode.layers.Lnum[idx2].keys();
		for a2 in imgPos10.size():
			var imgPos15 = imgPos10[a2];
			arrMax.append(imgPos15);
			
	
	var arrMax1_x = arrMax.max().x;
	var arrMax1_y = arrMax.max().y;
	
	var arrMin1_x = arrMax.min().x;
	var arrMin1_y = arrMax.min().y;
	
	var arrMin1 = arrMax.min()
	
	var format = PlugTileDrawNode.layers.Lnum[itemListNum].values()[0].get_image().get_format();
	var imgSelect1 = Image.create(arrMax1_x + abs(arrMin1_x)+32, arrMax1_y + abs(arrMin1_y), false, format);
	
	print("arrMax1_x: " + str(arrMax1_x) )
	print("arrMax1_y: " + str(arrMax1_y) )
	print("\n arrMax: \n" + str(arrMax) )
	
	for i in range(0, lays.size() ):
		var idx = lays[i]
		
		var imgPos = PlugTileDrawNode.layers.Lnum[idx].keys();
		var img11 =  PlugTileDrawNode.layers.Lnum[idx].values();

		
		for p in img11.size() :
			
			var imgPos2 = imgPos[p];
			var img12 =  img11[p];
			
			imgSelect1.blend_rect(img12.get_image(), Rect2(Vector2(0, 0), Vector2(32, 32) ), imgPos2 - arrMin1 );

So far i have X working ( image Width )
Is there a way to get the right ( image Height ) using the array max Y value ?
in here:
var imgSelect1 = Image.create(arrMax1_x + abs(arrMin1_x)+32, arrMax1_y + abs(arrMin1_y), false, format);

Can you check the values for arrMax.max() and arrMax.min()?
You said arrMax1_y was 192, which is not true because there are bigger y values inside the huge array.

But hear me out for a few seconds, what if arrMax.max() returns you the Vector2 with the highest combined values of x and y?

Would it therefore not make sense that arrMax.max() returns Vector2(480, 192) even if there might be a higher y value somewhere else?

arrMax1_x: 896
arrMax1_y: 192
arrMax.max(): (896, 192)
arrMax.min(): (-320, 64)

I was also thinking that could be the case…
I Will try to get the X and Y into a sperate array var, then check for max

I cant get it to work now the X stopped working ?


arrMax.clear();
arrMax_x.clear();
arrMax_y.clear();

for a1 in range(0, lays.size() ):
	var idx2 = lays[a1]
	var imgPos10 =  PlugTileDrawNode.layers.Lnum[idx2].keys();
	for a2 in imgPos10.size():
		var imgPos15 = imgPos10[a2];
		arrMax.append(imgPos15);
		arrMax_x.append(imgPos15.x);
		arrMax_y.append(imgPos15.y);

var arrMax1_x = arrMax_x.max();
var arrMax1_y = arrMax_y.max();

var arrMin1_x = arrMax_y.min();
var arrMin1_y = arrMax_y.min();

var arrMax1_x2 = arrMax1_x + abs(arrMin1_x)+32;
var arrMax1_y2 = arrMax1_y + abs(arrMin1_y)+32;

var arrMin1 = arrMax.min()

var format = PlugTileDrawNode.layers.Lnum[itemListNum].values()[0].get_image().get_format();
var imgSelect1 = Image.create(arrMax1_x2, arrMax1_y2, false, format);

print("arrMax1_x: " + str(arrMax1_x) )
print("arrMax1_y: " + str(arrMax1_y) )
print("\n")
print("arrMax1_x2: " + str(arrMax1_x2) )
print("arrMax1_y2: " + str(arrMax1_y2) )
print("arrMax_x.max():" + str(arrMax_x.max()))
print("arrMax_x.min():" + str(arrMax_x.min()))

print

arrMax1_x: 800
arrMax1_y: 448

arrMax1_x2: 1056
arrMax1_y2: 704
arrMax_x.max():800
arrMax_x.min():-320

for the (X) 800+320 should be 1120… + 32… 1152
var arrMax1_x2 = arrMax1_x + abs(arrMin1_x)+32;

I dont know the reason why he puts 1056 ?

arrMax1_x2: 1056

this is looking more confusing has i keep adding more vars…

I need you to explain to me what you want to accomplish with the max value.
Do you want arrMax to be Vector2(898, 480)?
Can’t you create a custom algorithm for that?

var max_X = 0
var max_Y = 0
for vec in arrMax:
    if max_X < vec.x:
        max_X = vec.x
    if max_Y < vec.y:
        max_Y = vec.y
var max_Array = Vector2(max_X, max_Y)

Put a breakpoint at var arrMax1_x2 = arrMax1_x + abs(arrMin1_x)+32 and calculate it. Also don’t panic, you don’t require putting ; in gdscript.

We can revert back to your prior state of

	for a1 in range(0, lays.size() ):
		var idx2 = lays[a1]
		var imgPos10 =  PlugTileDrawNode.layers.Lnum[idx2].keys();
		for a2 in imgPos10.size():
			var imgPos15 = imgPos10[a2];
			arrMax.append(imgPos15);

which populates your arrMax array with a lot of resolutions. You just want to find the largest width and height, right? My algorithm above should do that.

Ive found the problem on the X value:
it was here:

var arrMin1_x = arrMax_y.min(); # var arrMin1_x = arrMax_x.min(); 
var arrMin1_y = arrMax_y.min();

Iam trying to create an image width and height with the values on the array:
var imgSelect1 = Image.create(arrMax1_x2, arrMax1_y2, false, format);

the problem is the image cant be created if the numbers are negative…
i get this error:

The Image width specified (-288 pixels) must be greater than 0 pixels.
core/io/image.cpp:2878 - Condition “dsize == 0” is true.

so far i have the image working with negative values…

	var arrMax1_x2 = arrMax1_x + abs(arrMin1_x)+32;
	var arrMax1_y2 = arrMax1_y + abs(arrMin1_y)+32;
	
	
	var format = PlugTileDrawNode.layers.Lnum[itemListNum].values()[0].get_image().get_format();
	var imgSelect1 = Image.create(arrMax1_x2, arrMax1_y2, false, format);

The problem now is when the numbers are positive:

the problem is in this line:

	var arrMax1_x2 = arrMax1_x + abs(arrMin1_x)+32;
	var arrMax1_y2 = arrMax1_y + abs(arrMin1_y)+32;
	
	
	var format = PlugTileDrawNode.layers.Lnum[itemListNum].values()[0].get_image().get_format();

	var imgSelect1 = Image.create(arrMax1_x2, arrMax1_y2, false, format); #<--in here arrMax1_x2 and arrMax1_y2

Hes getting a Gap when the array position numbers are positive, in this case is adding +64.
the +vector( 32, 32 ) from the editor position witch is 0, plus more 32 from here:

	var arrMax1_x2 = arrMax1_x + abs(arrMin1_x)+32;
	var arrMax1_y2 = arrMax1_y + abs(arrMin1_y)+32;

if i remove the +32 at the end the negative position, he will have a cut of 32 in the final image size
I just cant make the + and - work togheter… Can you see by the video ?

this shjould work

for a1 in range(0, lays.size() ):
	var idx2 = lays[a1]
	var imgPos10 =  PlugTileDrawNode.layers.Lnum[idx2].keys();
	for a2 in imgPos10.size():
		var imgPos15 = imgPos10[a2];
		arrMax_x.append(imgPos15.x);
		arrMax_y.append(imgPos15.y);

var arrMax1_x = arrMax_x.max();
var arrMax1_y = arrMax_y.max();

var arrMin1_x = arrMax_x.min();
var arrMin1_y = arrMax_y.min();

var arrMax1_x2 =   abs(   abs( arrMax1_x )  - abs( arrMin1_x )  ) + 32;
var arrMax1_y2 =   abs(   abs( arrMax1_y )  - abs( arrMin1_y )  ) + 32 ;

var format = PlugTileDrawNode.layers.Lnum[itemListNum].values()[0].get_image().get_format();
var imgSelect1 = Image.create(arrMax1_x2, arrMax1_y2, false, format);

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.