I managed to make points via the print function appear in a circle shape and animated it to go in a constant rotation with the circle variable. However my attempt to auto arrange the points into a 1/number of points segments of the circle to make them laid out evenly without inputting the angle of each one seems instead to make them go around far more than 360 degrees around the circle, as it feeds into itself.
For example for 5 points I'd want each circle in 1/5th of the 360 degrees with even space on each side, which should make a regular pentagon shape if you joined up the dots
function love.load() --Only run at startup
cycle = 0
points = 9 -- should work on any value
radius = 0.5
love.window.setMode(90, 90)
end
function love.update()
cycle = cycle + 0.05
if cycle >= 360 then
cycle = 0
--prevent huge values
end
end
function love.draw()
i = 0
while i < points do
x = radius * math.deg(math.sin(cycle + (360 * (i / points )) ) )
y = radius * math.deg(math.cos(cycle + (360 * (i / points )) ) )
--cycle to move and i + 1 / points to auto arrange
b = (i / points )
b = round(b, 2)
love.graphics.print( b , 33 + x, 33 + y)
i = i + 1
end
end
function round(num, idp) --rounding function for display
local mult = 10^(idp or 0)
return math.floor(num * mult + 0.5) / mult
end
What currently happens:
In your loop, you're doing
x = radius * math.deg(math.sin(cycle + (360 * (i / points )) ) )
y = radius * math.deg(math.cos(cycle + (360 * (i / points )) ) )
whereas, you want to have:
b = i / points
local c = cycle + (360 * b) -- to lessen the computation cost
x = radius * math.sin( math.rad(c) )
y = radius * math.cos( math.rad(c) )
You just needed to convert that 360 to radians using the lua math.rad() function, as lua's math.sin(O) and math.cos() functions go by radians rather than degrees. the excalamation point have nothing to do with code and are just there to grab your attention to the problem.
x = math.deg(radius*math.sin(cycle + (!!!math.rad(360)!!! * (i / points )) ) )
y = math.deg(radius*math.cos(cycle + (!!!math.rad(360)!!! * (i / points )) ) )
You need to use cos for x and sin for y to get the right order:
x = radius * math.deg(math.cos(cycle + (360 * (i / points )) ) )
y = radius * math.deg(math.sin(cycle + (360 * (i / points )) ) )
Related
I have this new efficient raycasting code:
vectorX, vectorY = math.cos(r * math.pi/180), math.sin(r * math.pi/180)
wallDistanceX, wallDistanceY = v[3] - v[1], v[4] - v[2]
wallParallelX, wallParallelY = -wallDistanceY, wallDistanceX
rayX, rayY = v[1] - cam.x, v[2] - cam.y
Distance = (
(rayX * wallParallelX) + (rayY * wallParallelY))/(
(vectorX * wallParallelX) + (vectorY * wallParallelY)
)
if (
(math.min(v[1],v[3]) <= (cam.x + (Distance * vectorX)) and math.max(v[1],v[3]) >= (cam.x + (Distance * vectorX))) and
(math.min(v[2],v[4]) <= (cam.y + (Distance * vectorY)) and math.max(v[2],v[4]) >= (cam.y + (Distance * vectorY)))
) then
table.insert(possibles, Distance)
else
table.insert(possibles, 0)
end
This segment of code was converted to Lua from the answer of this post, but when the rays are drawn, they look broken up, like this:
I've done my debugging and so, and it turns out that when the ray is tested for intersection of a line, a distance of 0 is returned (a distance of 0 being returned means that the ray supposedly didn't intersect with a wall). There is a wall present as you see, and the minimap in the top-left corner shows just that.
Any ideas on why the ray is not being detected when it intersects? If you don't understand something pleeease ask.
r = angle for ray
v = an index of the list {}; v[i] is either the x1,y1,x2,y2 of the line
I'm having a mathematical issue that I can't get through.
I'm making a model browser in Lua where mouse movement is hooked to camera position, camera position has 2 modes: fixed and free, free works flawlessly whereas fixed seems to have issue with calculating proper Z.
X and Y is calculated properly and works without any issue, but Z seems to scale with X, Y way too much as seen here: http://puu.sh/oTN1v/5846343f82.webm (These camera warps happen whenever I click right mouse button, which happens even if I don't move mouse )
function self:RightMouseClick()
local cx, cy = mousepos()
local radius = math.sqrt( math.pow( campos.x, 2 ) + math.pow( campos.y, 2 ) )
local ang = ( camorigin - campos ):Angle()
function self:Think()
if input.IsMouseDown( MOUSE_RIGHT ) then
local x = camorigin.x + radius * math.cos( math.rad( 1 ) * ( 180 + ang.yaw + ( cx - mousex() ) * 0.5 ) )
local y = camorigin.y + radius * math.sin( math.rad( 1 ) * ( 180 + ang.yaw + ( cx - mousex() ) * 0.5 ) )
local z = camorigin.z + radius * math.sin( math.rad( 1 ) * ( ang.pitch + ( cy - mousey() ) * 0.5 ) )
campos = Vector( x, y, z )
end
end
end
#Edit: If you have no clue what this code means, you might aswell just tell me how to properly calculate Z for camera movement around axis
Removing camorigin from calculation of x, y, z worked out.
I have Hough-Transform implemented using Opencvsharp (opencv), and get the lines detected on my image in console application/windows-from-application:
lines = edgeImg.HoughLines2(storage, HoughLinesMethod.Probabilistic, 1, Math.PI / 180, 60, 100, 100);
for (int i = 0; i < lines.Total; i++)
{
CvLineSegmentPoint segP= lines.GetSeqElem<CvLineSegmentPoint>(i).Value;
double angle = Math.Atan2((segP.P2.Y) - (segP.P1.Y), (segP.P2.X) - (segP.P1.X)) * 180 / Math.PI;
if (Math.Abs(angle) <= 60)
continue;
if (segP.P1.Y > segP.P2.Y + 20 || segP.P1.Y < segP.P2.Y - 20)
src.Line(segP.P1, segP.P2, CvColor.blue, 2, LineType.AntiAlias, 0);
}
I have tried different methods for visualizing the rho-theta space. since "HoughLinesMethod" does all the transformation internally, I have tried to get these values from x,y in the reverse way:
double angle = Math.Atan2(dy, dx) * 180 / Math.PI;
double theta = 90 - angle;
var thetaRad = theta*Math.PI/180;
double rho = (x1 * Math.Cos(thetaRad) + y1 * Math.Sin(thetaRad));
my first question is if I need to get two values for rho/theta, both for x1,y1 and also x2,y2 ; or calculating only one "rho/theta" would be the right intersect?
Thanks!
second, how can I visualize them in the right format? (what I currently see on my outout image is some random white dots at the top left corner of my output)
third, is it rational to get rho,theta values back in this way or you would suggest to perform the hough transform by myself and reduce the complexity? (I used opencvsharp function for better and efficient performance!)
I am learning how to make a Julia Set fractal. I am using this as a reference.
I know the math theory behind it very well. I can compute it manually, too. However, what I do not understand is how it is being done in the program mentioned in the reference.
The author has certain variables that determine the zoom and displacement and he performs some calculations on it.
Can someone please explain what they are ?
Let's take a look at this line (the one below it works the same way):
newRe = (x - w / 2) / (0.5 * zoom * w) + moveX;
(Ignore the lack of 1.5 factor, that's just there to make sure it doesn't look "squished.")
It's in a for loop that assigns values between 0 and w to x.[1] So the leftmost and rightmost newRe values are going to be:
Leftmost:
newRe = (0 - w / 2) / (0.5 * zoom * w) + moveX;
= -(w / 2) / w / 0.5 / zoom + moveX;
= -(1 / 2) / 0.5 / zoom + moveX;
= -1 / zoom + moveX;
Rightmost:
newRe = (w - w / 2) / (0.5 * zoom * w) + moveX;
= (w / 2) / w / 0.5 / zoom + moveX;
= (1 / 2) / 0.5 / zoom + moveX;
= 1 / zoom + moveX;
Their difference -- that is, the width of the actual rectangle of the Julia fractal being displayed -- is equal to:
(1 / zoom + moveX) - (-1 / zoom + moveX)
= (1 / zoom) - (-1 / zoom)
= 2 / zoom
(This whole calculation also works for newIm, h, and moveY.)
This is why increasing zoom causes the rectangle we're examining to shrink -- which is exactly what "zooming in" is.
[1] It actually only goes to w-1, but that one-pixel difference makes this calculation a whole lot more difficult.
I better explain my problem with an Image
I have a contour and a line which is passing through that contour.
At the intersection point of contour and line I want to draw a perpendicular line at the intersection point of a line and contour up to a particular distance.
I know the intersection point as well as slope of the line.
For reference I am attaching this Image.
If the blue line in your picture goes from point A to point B, and you want to draw the red line at point B, you can do the following:
Get the direction vector going from A to B. This would be:
v.x = B.x - A.x; v.y = B.y - A.y;
Normalize the vector:
mag = sqrt (v.x*v.x + v.y*v.y); v.x = v.x / mag; v.y = v.y / mag;
Rotate the vector 90 degrees by swapping x and y, and inverting one of them. Note about the rotation direction: In OpenCV and image processing in general x and y axis on the image are not oriented in the Euclidian way, in particular the y axis points down and not up. In Euclidian, inverting the final x (initial y) would rotate counterclockwise (standard for euclidean), and inverting y would rotate clockwise. In OpenCV it's the opposite. So, for example to get clockwise rotation in OpenCV: temp = v.x; v.x = -v.y; v.y = temp;
Create a new line at B pointing in the direction of v:
C.x = B.x + v.x * length; C.y = B.y + v.y * length;
(Note that you can make it extend in both directions by creating a point D in the opposite direction by simply negating length.)
This is my version of the function :
def getPerpCoord(aX, aY, bX, bY, length):
vX = bX-aX
vY = bY-aY
#print(str(vX)+" "+str(vY))
if(vX == 0 or vY == 0):
return 0, 0, 0, 0
mag = math.sqrt(vX*vX + vY*vY)
vX = vX / mag
vY = vY / mag
temp = vX
vX = 0-vY
vY = temp
cX = bX + vX * length
cY = bY + vY * length
dX = bX - vX * length
dY = bY - vY * length
return int(cX), int(cY), int(dX), int(dY)