Logic behind creating a traffic light using sfml - graphics

I am trying to write a code to design a simple traffic light. However, I cannot figure out the maths behind it. Can someone help me with the logic?
saying, X is the length and Y is the width...I need to divide the rectangle into 3 equal parts and then position the coloured circles on it.

I think you could take the following design for your application.
Or if you don't want to take squares as building blocks:
However this could result in a bad geometry if you don't choose epsilon carefully.

Related

Concave Shape (Opposite of Convex Shape)

I am trying to draw a concave circle, e.g. something like a soup bowl to form the "pips" on a die in a Yahtzee like program I am working on using C#.
It seems to me that in graphics "concave" has a different, and quite complex, meaning to what I am trying to achieve - the opposite of convex.
I can draw quite a realistic ball using a PathGradientBrush with white at a position one third of the way across and one third down and SurroundColors of the ball colour and I had hoped by moving the white spot to two thirds across and down I might get what I want but sadly not.
Can anybody give me a steer please? Even a pointer to what might be better search term would be a good start.
Many thanks.

Dot Product - How does it help to define whether the light source hits my object or not?

For example the light source is coming from 1,3,-5 and object is at 4,-2,-1.
Algebraic formula is going to give the answer as 3. [1,3-5].[4,-2,-1]
= 1*4 + 3*-2 + -5*-1 = 3
But what does this 3 means? How do I know if my object is shaded with this number 3? Or is there more to it? I did look around and unable to find anything conclusive. Would be great if someone could give some insight. Thank you.
Judging from answers, pondering if I am understanding my question wrong. I was trying to get my head around the following question:
For a point on a convex surface, with the normal n=(n1,n2,n3)and light
direction l = (l1,l2,l3), determine if the point can be seen by light
source.
Using a dot product between two points makes no sense. Essentially, a dot product gives a measure of how similar two vectors are. When applied to points, the value will be related to the similarity of the direction to the points from the origin, as well as their distance from it. That metric doesn't make much sense, as you found out with that '3'.
To determine the amount of illumination, you want to be using a dot product between the normalized vector of the direction from the surface to the light and the surface normal. The result will be a value from -1 to 1, which you can interpret as an illumination factor for simple gouraud shading. In pseudocode:
illumination = max(0, dot(normalize(lightPosition - positionOnSurface), surfaceNormal))
Determining if a light hits an object is an entirely different problem called occlusion, and not really something you express in as mathematical formula. It's about testing what objects are in the path from the light to your target object.
The dot product can tell you on what side of a line a point is. The triangle is formed by three lines. If you are on the same side of all three lines then you are inside the triangle. You can use three dot products to test for each of the three sides. See slide 23 on this link http://comp575.web.unc.edu/files/2010/09/06raytracing1.pdf.

How to calculate a pixels world space position on an image plane formed by a virtual camera?

First, this Calculating camera ray direction to 3d world pixel helped me a bit in understanding what the virtual camera setup is like. I don't understand how the vectors work in this setup, and I thought normalized device coordinates had to be used which led me to this page http://www.scratchapixel.com/lessons/3d-basic-lessons/lesson-6-rays-cameras-and-images/building-primary-rays-and-rendering-an-image/. What I am trying to do is build a ray tracer, and as the question states, find out the pixels position in order to shoot out a ray. What I really, really really would like, is an actually example showing a virtual camera setup, screen resolution and how to calculate a pixels position, then transform to world space coordinates. Experts!, Thank you for your help! :D
Multiply a matrix by the coordinates. What matrix? There are lots of choices. For example XNA uses a projection matrix, view matrix and world matrix. Applying all of them transforms pixel coordinates into world coordinates or vice versa. Breaking it down this way helps to understand the different transformations going on so you can more easily construct the matrices.
Isn't this webpage providing you already with 4 pages of explanation on how these rays are built? It seems like you haven't made the effort to read the content of the link you are referring to. I would suggest you read it first, try to understand it, maybe look at the source code they provide and come back with a real question regarding what you potentially don't understand.
It's all there, and I am not going to re-write what these people seem to have put a lot of energy already to explain! (nor should anybody else really ...).

AI search - build a rectangle from 12 tetris shapes how many states are possible?

You have 12 shapes:
which you can make each out of five identical squares.
You need to combine the 12 pieces to one rectangle.
You can form four different rectangles:
2339 solutions (6x10), 2 solutions (3x20), 368 solutions (4x15), 1010 solutions (5x12).
I need to build the 3X20 rectangle:
My question what is the maximum number of states (i.e., the branching factor) that is possible?
My half way calculation:
The way I see it, there are 4 operations on each shape: turn 90/180/270 degrees and mirroring (turning it upside down).
Then, you have to put the shape on the board, somewhere on the 3X20 board.
Illegal states will be one that the shape doesn't fit in the board, but they are still states.
For the first move, you can chose each shape in 4 ways which is 4X12 ways, and then you need to multiply in the number of positions the shape can be in, and that is the number of states you have. But how can I calculate the number of positions?
Please help me with this calculation it is very important, it is not some kind of homework which I'm trying to avoid.
I think there is no easy & 'intelligent' way to list solutions (or states) to pentomino puzzles. You have to try all possibilities. Recursive programming or backtracking is the way to do it. You should check this solution that also has java source code available. Hopefully that points you to the right direction.
There is also a python solution that is perhaps more readable.

Vector math and rectangles

I know this is more high school math(wow been a long time since I was there) but I am trying to solve this programatically so I am reaching out to the collective knowledge of stackoverflow
Given this layout:
Midpoint is my reference point and in an array I have the vector points of all other points (P)
I can get to this state with code of having the light blue area by breaking it into four quadrants and doing a lame bubble sort to find largest(y) or lowest(x) value in each quadrant.
I need to find only the quadrants that outer border fully hits red no white space. For example the lower left and the up right dont have any white space hitting the light blue rectangle.
I am sure my terminology is all off here and im not looking for any specific code but if someone could point me to a more optimized solution for this problem or the next step in what I already have.
Thank you
I might do some BFI solution first, then perhaps look to generalize it or at least reduce it to a table-drive loop.
So, if it's exactly these shapes, and not a general solution, I think you should proceed sort of like this:
Derive the coordinates of the blue rectangle. I suspect one thing that's confusing you is that you have each individual x and y for the blue rect but you can't easily loop through them.
Derive the coordinates of the midpoint of each rectangle edge. You are going to need this because you care about quadrants. It will be trivial to do this once you have done 1.
Write different code for each 1/2 rectangle edge. There is no doubt a more clever way but this will get working code.
Make it more elegant now if you care. I betg you can reduce the rules to an 8-row
table full of things like 1, -1, or something like that.
First, you can't define red area by a single vector, since it's disjoint. You need the same number of vectors as the number of distant red regions.
Second, do we assume that different red figures neither intersect nor share a border? In the next clause I do.
Third, under assumption in point 2, the quadrant will have all red outer side iff there exists a contiguous red figure that intersects both its axes (i.e. rays). To determine this for all quadrants, you should only traverse all (P) points in the order they're given. This takes linear time and solves the problem.

Resources