Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
i am trying to implement a program that solves the "Unblock me" puzzle. For those who dont know it, "Unblock me" is a sliding block puzzle where you have a board with 1x1, 1x2, 2x1 and 2x2 boxes and the goal is to move the red box out of the board through an opening.
My question is how can i represent a board state? I searched throught the internet and found a suggestion to represent the state like this.
For example this board: Board State
Representation: Grid representation
I can understand how this representation works. The problem comes when i want to move one of the boxes. How can i check if a box is 1x1, 1x2, 2x1 or 2x2 in order to check if the wanted move is possible or not??
Any ideas on how i could do that, or any other "easier" representations?
I would do:
Width of grid
Height of grid
List of boxes, and for each box:
Dimensions of the box (doesn't have to be a square or rectangle)
Width of the box
Height of the box
Each of the cells that are occupied within the bounds of the box
Position of top-left cell, so coordinates (x, y)
Name of the box (e.g., "A", "B", "C")
If this is an unblock puzzle where you need to slide a box outside the grid, you'll also need:
Coordinates of the grid cell that has the exit
The position of the exit for that cell (e.g., "north", "east", etc). This is necessary when the exit is located in a corner cell.
This should allow you to perform any computations as necessary. Given the top-left cell of where a box is located, the dimensions of the box and knowing what cells are occupied within the box, you can compute whether a collision is taking place or not.
The above representation also supports arbitrary shapes, not just rectangles and squares.
You could always have an array of the same dimensions as the "grid", the exit would have a special value of -1, empty squares have 0 and each block would have the number of the block.
You could then have a Check function that takes a block number and a direction and for each current cell that has the block number as the value check that the adjacent cell either has the same block number or is <1.
The move function would shift the values in the required direction setting zero, with a little care on the order things are done you can just start with the first set the cell in the required direction to the required number and the current square to 0. Minimal ifs and a couple of loops.
Related
I have coordinates for 2 corners https://prnt.sc/w2jryh (x and y coordinates for d and b points of the square). And I need to create screenshot within the area of this square, but when I am trying to do that, it is failing, either getting too much in screenshot, or too less. What may be the magic formula for that :) This is what I tried:
pyautogui.screenshot("testScr.png",region=(blackRookCornerX,whiteRookCornerY,whiteRookCornerX,blackRookCornerY))
basically taking coordinates and trying get the right screenshot. Coordinates are correct here.
From their docs
There is also an optional region keyword argument, if you do not want a screenshot of the entire screen. You can pass a four-integer tuple of the left, top, width, and height of the region to capture:
The first two numbers should be the x,y coordinates of the top left corner of where you want to take a shot, the third number is how far right/left to go (in pixels) and the fourth is how far up/down to go (in pixels).
Try this:
pyautogui.screenshot("testScr.png", region=(blackRookCornerX, whiteRookCornerY, 100, 100))
Start with a broad number like 100 and then slowly whittle away until you have the perfect screenshot.
You could make a hotkey to use for each corner, to collect the coordinates; Simply put your mouse in those corners and press each hotkey. Then once you have done that for both corners and have two variables, use those variables for your screenshot.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am trying to detect the center&radius of an arc like shown below for my thesis by using open cv. I tried many things and searched a lot, but cant figure out. Could please somebody help me? I would be really glad.
I would not do the center finding itself with OpenCV but with simple 2D geometry instead see first bullet in Circular approximation of polygon (or its part) so:
filter out blobs not on curve
segmentate and remove too small (unconnected) blobs
find 3 points on your curve
They should be far enough from each and should form 2 lines (black). You can apply thinning algorithms to enhance precision. than simply find 2 most distant points from the curve and one that is in half way between them.
cast normal axises from their mid points (brown)
simply rotate the line slope by 90 deg by swapping x,y of direction vector and negating one of them so (-y,x) or (y,-x).
find the intersection its the center you are looking for
find radius
its the average of distance between center and the 3 points ...
Here a small example I just did in paint (its hand drawn so not pixel perfect):
Here is my simple approach algorithm:
Look at the angle contour by wide-view, like:
Check each pixel of this wide-view image one by one and find the norms(lengths) for each point of the contours. (To be clear: for each pixel, find lengths to those contour points)
If all lengths are equal for a pixel then that pixel is the center of the circle.
Note: This is simple approach and absolutely works. Just not sure about does it take long time to calculate for cpu.
Can you please suggest some effective and efficient ways first to find coordinates (lower left corner and upper right corner) of square in a chess board picture and then save it in some data structure with some key.
For example : saving coordinates of bottom left most box in a data-structure with key “a1”
Please inform me if you need any other information from me.
enter image description here
Description of project
In my project I have:
work place: designed in form of chess board
On this chess board (used as work place) robot is allowed to move and do his work.
My task is to design a software using opencv and python to find location of robot in term of square number in chess board. Till now I am able to find location of robot in form of x and y coordinate on chess board but now want to translate these coordinates in terms of square on chess board where robot is standing. In order to do that I need a data structure which contain coordinates of each square in chess board with their keys e.g "a1" square coordinate is (0,0) and (1,1).
So, I want to know
a) how to find coordinates of square in chess board image using opencv and python.
b) Which datastructure will be suitable to save these coordinates of squares on chess board image with their identification number
Please inform me if I can provide any other information.
It was really helpful if we'd known the purpose of the question, why you wanna do it? I'll assume you're trying to create a chess game-
I suggest you have 2 pictures:
*the board
*its additions (letters and numbers)
Then, create a 2d array of 8x8 that represents the board itself where every var inside this matrix is the id of a piece,
and when you want to draw just calculate according to the size of the board where the piece should go.
For example, let's say the board is 2x2, our board is 200x200 pixels and the piece id is 1:
- [[1,],[,]] I'll create a function that parses this matrix and passes the coordinates so the piece will be drown inside the bottom left square,
i.e- (0,100) or something like that
- And step two will be to take the coordinates we got from before and just draw the piece on those coordinates.
Hopefully, I got it right assuming you're trying to create a chess game and if not a clarify would be helpful! (:
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I've been trying to attempt a Yin-Yang circle on Python, but so far I've only been able to do a black semi-circle. In here I've attached what it looks like for me and what it's supposed to look like. What else should I add to my code?
*to make it easier for me to understand, use fill function when coloring.
My Code:
import turtle
turtle.begin_fill()
turtle.circle(50,180)
turtle.end_fill()
turtle.hideturtle()
What it looks like for me:
results
How it's supposed to be:
desired results
You are pretty close already, if you think of the yin-yang symbol mathematically, it's just a bunch of arcs, two large ones on the outside, and two on the inside, half the length.
If the outside arcs have a length of 50, then the internal arcs need to be 25. Turtles move counter-clockwise by default, so to get a clockwise curve, use a negative radius. Like so:
turtle.begin_fill()
turtle.circle(50, 180)
turtle.circle(25, 180)
turtle.circle(-25, 180)
turtle.end_fill()
turtle.circle(-50, 180)
After that, the turtle will back where it started, and the positions for the circles should be roughly a quarter and three quarters of 50 directly below the turtle, where you just need to make two full circles.
As much as I like #CameronFerguson's single curve approach (+1), it might be easier for a beginner to think of this as simply five circles. The first, largest circle is filled on one side, as you have already achieved. The next two circles are half the radius of the original, centered on the vertical axis, and opposite colored:
Choose a size for the smallest two circles, above they are 1/8th of the original radius, use the same center point as the previous circles and reverse the color again. Now, switching to the final colors, and raising the pen appropriately to avoid unwanted lines, we get:
With some additional thought, the two sets of inner circles can done in a loop, cutting the number of calls to turtle.circle() in half. But if you're comfortable with #CameronFerguson's curve approach, go with that!
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
The player in my game is centered in the screen, and I 'scroll' the background to move around.
I get a list of keys pressed with pygame.key.get_pressed(), if the player's move cooldown is over, I call a move function. This function scrolls the map in the direction of movement for one tile.
However the movement is not smooth. It 'jumps' to the next tile.
How do I implement a movement system so that the player moves in small steps (speed * dt) but still only moves the distance of one tile every time the move function is called? The game needs to be rendered in between each small step I think.
Full source here: https://github.com/HCollings/rpg-game/blob/alternate_movement/game.py
Animations and movement are all about what you perceive rather than what's actually happening. In a grid system, you can abstract the visual movement of the player away from the actual mechanics by taking the drawing part of the code and updating the draw position independently of the gameplay position.
The problem you are experiencing with jumping is commonly handled with some kind of interpolation technique. lerp() is a common function that provides linear interpolation, but in my below example I will use a different technique.
Here is a resource about interpolation techniques: http://paulbourke.net/miscellaneous/interpolation/
You could implement your movement as destination, direction, and "unspent" increments of movement.
If you wanted to move to a new square, you would set your direction and add a standard number of increments (representing one square) to your "unspent increment counter". Then you draw your graphic at the new location offset by the number of increments you still have. If you want to move more than one square at a time, then you would increase the number of unspent increments.
Movement would only take place so long as you have unspent increments (think movement points). This technique also lends itself to visual collision detection; if the remaining number of increments is some fraction of the total increments (~25%?) between the number of squares you are moving, then the graphic is "practically" in the square according to what the player sees.