some problems about color flipping game only made by python turtle - python-turtle

I am asked to make a "color flipping" game,which just can be made by python turtle......
I met some problems these days.
1.when your mouse clicks the square,the square will be surrounded by a block border,but if you click another square,the previous block border will disappear and surround the last clicked one.
enter image description here
2.when you click the square and then click the color set at the bottom,the square color will change. If the surrounding squares' color are the same,they will change together.
I don't know how to solve it just using turtle module......
I have created the squares and the color set correctly I think,
turtle_dict = {}
for i in range(5):
for j in range(5):
turtle_dict["turtle" + str(i) + str(j)] = turtle.Turtle("square")
turtle_dict["turtle" + str(i) + str(j)].shapesize(3, 3, 1)
turtle_dict["turtle" + str(i) + str(j)].speed(10000)
turtle_dict["turtle" + str(i) + str(j)].up()
turtle_dict["turtle" + str(i) + str(j)].setpos(27+70*i, 29+70*j)
turtle_dict["turtle" + str(i) + str(j)].down()
turtle_dict["turtle" + str(i) + str(j)].color('white', randomColor())
but I can't select the square and color set and I also don't know how to judge the surrounding square color......
thank you for your advice!

Related

Can't draw electric field dipole

I am trying to simulate electric field dipole by using Maxima.
Here is the code:
P : 1$
eps0 : 8.854$
Ex : 3*P*y*x/(4*π*eps0*(x^2 + y^2)^2.5)$
Ey : P*(3*y^2/(x^2 + y^2) + 1)/(x^2 + y^2)^1.5/(4*π*eps0)$
ew : sqrt(Ex^2 + Ey^2)$
contour_plot (ew,[x, -4, 4], [y, -4,4]);
But i am getting only straight lines.
What can be wrong, because similar code in Wolfram Mathematica works fine.
contour_plot is not as smart as the corresponding function in Mathematica. You can help it a bit.
P : 1$
eps0 : 8.854$
Ex : 3*P*y*x/(4*%pi*eps0*(x^2 + y^2)^2.5)$
Ey : P*(3*y^2/(x^2 + y^2) + 1)/(x^2 + y^2)^1.5/(4*%pi*eps0)$
ew : sqrt(Ex^2 + Ey^2)$
BIG: subst([x=1/4, y=1/4], ew)$
cap_log(e, c):= if e > c then log(c) else log(e)$
set_plot_option ([gnuplot_preamble, "set cntrparam levels 8"])$
contour_plot ('cap_log(ew, BIG), [x, -4, 4], [y, -4, 4])$
You can also use plotdf.
(%i1) [P,eps0] : [1,8.854]$
(%i2) Ex : 3*P*y*x/(4*%pi*eps0*(x^2 + y^2)^2.5)$
(%i3) Ey : P*(3*y^2/(x^2 + y^2) + 1)/(x^2 + y^2)^1.5/(4*%pi*eps0)$
(%i4) plotdf([Ex,Ey],[vectors,"blank"]);
Click on some points in the plot, then enter the Plot Setup menu (the icon with a wrench and a screw driver), erase the color "red" in fieldlines and choose a color, for instance "blue", in curves. Return to the plot by clicking in Ok, and click again in the plot to trace a few equipotential curves.

finding the rth term of a sequence

the question is to give a possible formula for the rth term.
i'm able to solve two questions but rest i can't seems to be of a different way or like weird.as i'm studying alevels i think there's a common rule or maybe an easy way to solve sequence related problems.i never understood sequence well enough-it's just that hard for me.
6 18 54 162
i'm able to solve it by 2*3^r
4 7 12 19
by r^2+3
but
4 12 24 40 60
i'm trying so many ways but i can't find the answer.i think there's a common rule for solving all these not much marks are there so it should be solved in an easy way but i'm not getting how to.please help
Here's a formula in R for the sequence:
g <- function(n) 6*n + 2*n^2 + 4
g(0:4)
[1] 4 12 24 40 60
Here is one way to solve this relation. First, recognize that it is quadratic as the difference is an arithmetic sequence (linear).
Then note that g(x + 1) = g(x) + 8 + 4x. Represent g(x) = a*x^2 + b*x + c.
Then:
g(x+1) = a(x+1)^2 + b(x+1) + c = g(x) + 8 + 4x = a*x^2 + b*x + c + 8 * 4x
ax^2 + 2ax + a + b*x + b + c = a*x^2 + b*x + c + 8 + 4x
Thus
2ax + a +b = 8 + 4x
As this holds for all x, it must be that 2ax = 4x or a = 2. Thus
4x + 2 + b = 8 + 4x
So b = 6. With these known, c is determined by g(0) = c = 4.

If function not working in case of numeric characters

So i have used the following code
If (txtbedroom + txtkitchen + txtbathroom + txtlivingroom + 0) > 9 Or (txtbedroom + txtkitchen + txtbathroom + txtlivingroom + 0) < 7 Then
If MsgBox("Please ensure the number of rooms you entered is entered correctly.", vbYesNo) = vbNo Then
Exit Sub
End If
End If
even if the result of the following is less than 9 and greater than 7 the if function still gets satisfied... what can be done??
Anyway I solved the problem. I used a variable and the code CInt() like x = CInt(txtbedroom) to convert the text to number and then it worked...

Aggregating neighbouring pixels Python / GDAL and Numpy without interpolation

Consider we have an image of 2000 x 2000 pixels and the pixel size is 10 x 10 meters. The pixel values are also float numbers ranging from 0.00 - 10.00. This is image A.
I would like to resize image A to a quarter of its dimensions (i.e. 1000 x 1000 pixels) with a pixel size 20 x 20 meters (image B) by spatially aggregating four neighbouring pixels in non-overlapping blocks, starting from the top-left corner of the image, while the value for each pixel in image B will be a result of their arithmetic average.
I have written the following code using several sources from stackoverflow; however for some reason that I do not understand the resulting image (image B) is not always written properly and it is not readable by any of the software that I want to process it further (i.e. ArcGIS, ENVI, ERDAS etc).
I would appreciate any help
Best regards
Dimitris
import time
import glob
import os
import gdal
import osr
import numpy as np
start_time_script = time.clock()
path_ras='C:/rasters/'
for rasterfile in glob.glob(os.path.join(path_ras,'*.tif')):
rasterfile_name=str(rasterfile[rasterfile.find('IMG'):rasterfile.find('.tif')])
print 'Processing:'+ ' ' + str(rasterfile_name)
ds = gdal.Open(rasterfile,gdal.GA_ReadOnly)
ds_xform = ds.GetGeoTransform()
print ds_xform
ds_driver = gdal.GetDriverByName('Gtiff')
srs = osr.SpatialReference()
srs.ImportFromEPSG(26716)
ds_array = ds.ReadAsArray()
sz = ds_array.itemsize
print 'This is the size of the neighbourhood:' + ' ' + str(sz)
h,w = ds_array.shape
print 'This is the size of the Array:' + ' ' + str(h) + ' ' + str(w)
bh, bw = 2,2
shape = (h/bh, w/bw, bh, bw)
print 'This is the new shape of the Array:' + ' ' + str(shape)
strides = sz*np.array([w*bh,bw,w,1])
blocks = np.lib.stride_tricks.as_strided(ds_array,shape=shape,strides=strides)
resized_array = ds_driver.Create(rasterfile_name + '_resized_to_52m.tif',shape[1],shape[0],1,gdal.GDT_Float32)
resized_array.SetGeoTransform((ds_xform[0],ds_xform[1]*2,ds_xform[2],ds_xform[3],ds_xform[4],ds_xform[5]*2))
resized_array.SetProjection(srs.ExportToWkt())
band = resized_array.GetRasterBand(1)
zero_array = np.zeros([shape[0],shape[1]],dtype=np.float32)
print 'I start calculations using neighbourhood'
start_time_blocks = time.clock()
for i in xrange(len(blocks)):
for j in xrange(len(blocks[i])):
zero_array[i][j] = np.mean(blocks[i][j])
print 'I finished calculations and I am going to write the new array'
band.WriteArray(zero_array)
end_time_blocks = time.clock() - start_time_blocks
print 'Image Processed for:' + ' ' + str(end_time_blocks) + 'seconds' + '\n'
end_time = time.clock() - start_time_script
print 'Program ran for: ' + str(end_time) + 'seconds'
import time
import glob
import os
import gdal
import osr
import numpy as np
start_time_script = time.clock()
path_ras='C:/rasters/'
for rasterfile in glob.glob(os.path.join(path_ras,'*.tif')):
rasterfile_name=str(rasterfile[rasterfile.find('IMG'):rasterfile.find('.tif')])
print 'Processing:'+ ' ' + str(rasterfile_name)
ds = gdal.Open(rasterfile,gdal.GA_ReadOnly)
ds_xform = ds.GetGeoTransform()
print ds_xform
ds_driver = gdal.GetDriverByName('Gtiff')
srs = osr.SpatialReference()
srs.ImportFromEPSG(26716)
ds_array = ds.ReadAsArray()
sz = ds_array.itemsize
print 'This is the size of the neighbourhood:' + ' ' + str(sz)
h,w = ds_array.shape
print 'This is the size of the Array:' + ' ' + str(h) + ' ' + str(w)
bh, bw = 2,2
shape = (h/bh, w/bw, bh, bw)
print 'This is the new shape of the Array:' + ' ' + str(shape)
strides = sz*np.array([w*bh,bw,w,1])
blocks = np.lib.stride_tricks.as_strided(ds_array,shape=shape,strides=strides)
resized_array = ds_driver.Create(rasterfile_name + '_resized_to_52m.tif',shape[1],shape[0],1,gdal.GDT_Float32)
resized_array.SetGeoTransform((ds_xform[0],ds_xform[1]*2,ds_xform[2],ds_xform[3],ds_xform[4],ds_xform[5]*2))
resized_array.SetProjection(srs.ExportToWkt())
band = resized_array.GetRasterBand(1)
zero_array = np.zeros([shape[0],shape[1]],dtype=np.float32)
print 'I start calculations using neighbourhood'
start_time_blocks = time.clock()
for i in xrange(len(blocks)):
for j in xrange(len(blocks[i])):
zero_array[i][j] = np.mean(blocks[i][j])
print 'I finished calculations and I am going to write the new array'
band.WriteArray(zero_array)
end_time_blocks = time.clock() - start_time_blocks
print 'Image Processed for:' + ' ' + str(end_time_blocks) + 'seconds' + '\n'
end_time = time.clock() - start_time_script
print 'Program ran for: ' + str(end_time) + 'seconds'

How to mininize expression using boolean algebra?

Here is expression that we have to minimize with boolean algebra:
y = /A/B/C/D + /A/B/CD + /AB/CD + A/B/C/D + A/B/CD + A/BC/D + A/BCD.
I know little bit about that, please help!!!
The answer is /A/CD + /B/C + A/B
You need to put those minterms on a Karnaugh Map and find the minimized form after grouping.

Resources