I'm kind of curious how to write a Code that displays a hollow rectangle in Dr.Racket. I really don't have any clue, because I just started practicing some Racket. Hope someone can give some hints.
One way to repeat something is to use a for-loop.
(for ([i 5])
(display "*"))
(newline)
will display five stars followed by a new line.
You can use this to make the top and bottom of your rectangle.
(for ([i 5])
(displayln "* *"))
Will display the string "* *" times and change line after each.
You can use that to make the sides of the rectangle.
Related
Print all possible paths from top left to bottom right of a mXn matrix
The problem is to print all the possible paths from top left to bottom right of a mXn matrix with the constraints that from each cell you can either move only to right or down.
To find all the paths from (k,l) to (m,n), use recursion.
find all the paths from (k, l+1) to (m,n); add (k,l) to the front of each one.
find all the paths from (k+1, l) to (m,n); add (k,l) to the front of each one.
the paths from (m,n) to (m,n) is [].
Add handling of the boundaries.
You do not need to store a matrix in any form.
The question is to print the number of ways chords can be drawn in a circle having 2*n points such that no two chords intersect one another
Can someone explain how the Two recursions inside for loop working using a recursive tree diagram?
THE CODE OF THE PROGRAM
Here's the key idea of the algorithm implemented in the code you linked to:
If you place one chord, the non-intersection requirement means you've cut up your circle into to "circles", of smaller sizes, and you now need to place chords within each of them independently. The number of options for doing so is the multiplication of the number of options for each of two circles, due to the independence.
This problem is different from testing if one rect is in another rect.
Known information is the sides length of two rects.
How to calculate if one rect can be put into another rect?
This is a great question! If and only if one of these conditions is satisfied does a smaller rectangle with sides p and q (p >= q) fit completely into a larger rectangle with sides a and b (a >= b):
or
See this for reference.
So if we had variables a, b, p, q, we could check if such a rectangle arrangement would be possible by evaluating:
(p <= a && q <= b) || (p > a &&
b >= (2*p*q*a + (p*p-q*q)*sqrt(p*p+q*q-a*a)) / (p*p+q*q))
EDIT: Thanks to #amulware for posting this alternate version in his comment:
The first check one would do is of course whether the rectangle fits inside the other in either of the axis aligned orientations.
If not, the only option for it to fit is diagonally, but there might actually be many angles for which it fits, the difficulty is, not just guessing but indeed calculating a possible angle, if one exists.
Now, notice that if the inner rectangle does indeed fit diagonally, then you can rotate it until two if its opposite corners touch either both the top and bottom edge of the outer rectangle, or the left and right. (In your diagram more or less the first.)
In that case you already know that you have fit it inside in that one dimension(in the example, the y-axis). You then have to calculate the bounding width of the inner rectangle in the other dimension and check that against the width of the outer box.
There might be a smarter algorithm out there for this, but I am 100% sure that what I describe works. Let me know if you can figure out the math for this yourself(if you think this is a good solution), if not, I might have a go at it later. I wonder if my algorithm can be implemented completely without trig functions...
EDIT:
Ok now, I could not resist...
Here is the math I did to solve the problem as outlined above:
(Sorry, only in image form, I hope my handwriting is readable.)
I would be happy if someone could check my math. I do not see anything wrong with any of the steps right now, but it is always better to have someone else check.
(And of course: Use this at your own risk.)
If anyone finds anything wrong with this algorithm, please let me know and I will fix it as soon as possible.
Also I would be highly interested to see if anyone has a better solution, involving less complicated math. Maybe a vector based approach?
Well, it looks like A.R.S. solution is true, still I'll try to post my solution, it's harder, but it'll let you to build a concrete embedding of one rectangle into another (if possible).
Let us suppose that a>b and p > q. Solution is obvious if a > p and b > q. The problem can also be solved if a<p and b>q. Take a look at the attached photo, in it you'll need only last system of inequalities (if you interested you can look how it was derived)
All you need is to make sure that last system of inequalities has a solution lying between 0 and 1. To do it you need to solve each inequality as equation (as usual quadratic equation). If there are no solution (that's improbable) the solution of inequality is whole real line. If equation has two (maybe equal) solutions t_1 and t_2 the solution of inequality is segment [-infinity, t_1] united with [t_2, infinity]. After you got solutions of both inequalities you should intersect them. Now we should recollect that t is cos of an angle (between 0 and pi/2), so inequality should have solutions between 0 and 1. In that case second rectangle can be embedded into first one. And if you take e.g. t_1 (smaller root of equations) you can build a concreate embedding of rectangles.
You can weed out the two simple cases fairly easily:
If the larger dimension of the second is smaller than the larger dimension of the first, and if the same is true for the smaller dimensions, then the second fits inside.
If the larger dimension of the second is greater than the hypotenuse of the first, then the second will not fit in the first.
The hard part is working out whether it can fit in at an angle such as in your sketch. I don't know of a simple formula -- it probably requires a plug-and-chug solution.
Might be a good question for the Mathematics Stack Exchange site.
Added: I'm not 100% sure if this, but I think that if the hypotenuse of the second is smaller than the hypotenuse of the first then it will fit.
Oops: Nope -- I'll take that back. But if the hypotenuse of the second is larger than the hypotenuse of the first it won't fit.
I want to generate a cube where each face is divided into bits, like the following image:
http://img59.imageshack.us/img59/2504/gridcube165c3.jpg
Now, I can do this pretty simply if I'm just rendering quads, by just spacing vertices along each face plane at regular intervals, but my problem comes in when I want to turn the whole thing into a triangle strip. I've just got no idea how to unwrap it programmatically- is there some pattern to unwrapping that I'd follow?
I'm thinking of starting with the vertex at the top left corner as Row 0 Column 0 (R0C0), I'd want (first triangle) R0C0, ROC1, R1C1, (second triangle) R0C0, R1C0, R1C1 and so forth, and then when I reach the end of a row I guess I'd use a degenerate triangle to move to the next row, and then when I reach the end of the face I'd do the same to start a new face.
My main problem is that I can't visualize the program loop that would do this. I can reason out which vertex comes next visually, which is how I worked out the order above, but when I try to think programmatically I just stare blankly.
Even worse, with the end product I want the generated cube to be UV-mapped with a simple cube-map unwrap (the kind that looks like a T or t).
I guess, really, the best solution would be to find a library that already does this for me.
You could take a look at Ignacio CastaƱo's 'Optimal Grid Rendering' even though it's not triangle strips, it may inspire you.
Otherwise, you could use NVTriStrip library and be done with it.
I'm solving the classical eight queens problem in prolog. And now I'm about to start coding the bit that removes reflections about the horizontal line. Y=4. Say if the point (3.3) is reflected, the new point would be (6,6), is that correct? Another example, (2,1) would become (7,8).
The ones you show are not reflections but rotations.
The reflection along the vertical center-axis (flip left-right) of a square (x,y) is given by (9-x,y) (assuming 1<=x,y<=8).
The reflection along the horizontal center-axis (flip top-bottom) is given by (x,9-y).
The reflection by both axes (aka rotation) is given by (9-x,9-y)