FB CAD

User projects written in or related to FreeBASIC.
Post Reply
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

Yeah, then you can combine the terms, like this:

Code: Select all

  ( (1 / rx^2) + x^2 * (1 / s^2)     ) * x^2
+ ( -(2 * x0 / rx^2) - (2 * k / s^2) ) * x
+ ( (x0^2 / rx^2) + ( k^2 / s^2) - 1 )
= 0
Now you have it in "A*x^2 + B*x + C = 0" form, which you can solve with the quadratic equation.

x = ( -B +/- sqr(B^2 - 4 * A * C) ) / (2 * A)

Note that if the contents of the square root bit is less than zero, that means the line doesn't intersect with the ellipse at all.
If it is exactly zero, that means the line just touches the edge of the ellipse.
If it is greater than zero, that means the line goes right through it.
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

man I suck at math
owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

ellipse line intersection function almost got it

Post by owen »

this seems to be working. i will add it to my posted code soon
pending however is the case of a rotated ellipse
the math in this code calculates the intercting points of a line thru an ellipse in 2d.

the key i was looking for was the A,B,C values of the quadratic setup
A1 = (1 / (dy1/dx1))^2 + (cr1 / cr2)^2
B1 = (2 / (dy1/dx1)) * ((ax1-cx1) - (ay1-cy1) / (dy1/dx1))
C1 = ((ax1-cx1) - (ay1-cy1) / (dy1/dx1))^2 - cr1^2

Code: Select all

	ax1=lines(aintersect,1)'ax1,ay1 are the x,y values of one end of line
	ay1=lines(aintersect,2)
	ax2=lines(aintersect,4)'ax2,ay2 are the x,y values of the of the other end of the line
	ay2=lines(aintersect,5)
	cx1=circles(bintersect,1)'cx1,cy1 are the values of the center of the ellipse
	cy1=circles(bintersect,2)
	cr1=circles(bintersect,4)'cr1,cr2 are the major & minor radius values of the ellipse
	cr2=circles(bintersect,8)
	dx1 = ax2 - ax1'dx1 is the X distance between the end points of a line
	dy1 = ay2 - ay1'dy1 is the Y distance between the end points of a line
	A1 = (1 / (dy1/dx1))^2 + (cr1 / cr2)^2
	B1 = (2 / (dy1/dx1)) * ((ax1-cx1) - (ay1-cy1) / (dy1/dx1))
	C1 = ((ax1-cx1) - (ay1-cy1) / (dy1/dx1))^2 - cr1^2
	det=B1^2 - 4 * A1 * C1
	intersection="outside"
	If dy1=0 Then
		If ay1-cy1<=cr2 Then
			intersection="inside"
			fym1=(ay1-cy1)
			fym2=(ay1-cy1)
			fxm1= cr1 * Sqr(1 - ((ay1-cy1) / cr2)^2)
			fxm2= -fxm1
		End if
	Else
		If det>0 Then
			intersection="inside"
			det=Abs(det)
			fym1 = (-B1 + Sqr(det)) / (2 * A1)
			fym2 = (-B1 - Sqr(det)) / (2 * A1)
			fxm1 = (fym1 - (ay1-cy1)) / (dy1/dx1) + (ax1-cx1)
			fxm2 = (fym2 - (ay1-cy1)) / (dy1/dx1) + (ax1-cx1)
		EndIf
	End If
	If intersection="inside" then
		fxm1=fxm1+cx1
		fxm2=fxm2+cx1
		fym1=fym1+cy1
		fym2=fym2+cy1
		if sqr((mousex-fxm1)^2 + (mousey-fym1)^2) < sqr((mousex-fxm2)^2 + (mousey-fym2)^2) Then
			fxm=fxm1
			fym=fym1
		Else
			fxm=fxm2
			fym=fym2
		End If
	End if
owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

got it - line ellipse intersection and rotated ellipse

Post by owen »

2d line ellipse intersection function is done
it now includes the case of a rotated ellipse

code is in:

Sub calclineellipseintersection()

counting_pine and others, thanks for helping
Last edited by owen on Aug 01, 2013 14:34, edited 1 time in total.
MystikShadows
Posts: 612
Joined: Jun 15, 2005 13:22
Location: Upstate NY
Contact:

Post by MystikShadows »

Brilliant...this is really becoming a very usable program Owen.

Absolutely awesome work :-)
owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

circlecircleintersection

Post by owen »

calc intersection points of two circles is done

just notice a problem with ellipse line intersection in the case where the ellipse is rotated between 90 and 270 degrees. for some reason the detected points are opposite. so back to the drawing board.

i think it's a simple matter of rotating the line no more then 180, ie. rotate the line ellipserotationamount MOD 180.

well see.
owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

ellipse line intersection is fixed

Post by owen »

the rotation problem is fixed now.
it wasn't a rotation problem after all. it was a matter of comparing the mouse (x,y) to the two intersection points to see which was closer.
where as i was comparing those distances PRIOR to rotating the calculated points back.

so the correction was to compare AFTER rotating back.

next goal is to calc circle ellipse intersection points
owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

intersect two ellipses

Post by owen »

In order to calc the intersection point(s) of two ellipses, I resorted to use an iterative process. The precision is increased by adjusting the For Next loop (For anglerange=10 To .01 Step -.01) which can be found in Sub calcellipseellipseintersection()

For example:
(For anglerange=10 To .01 Step -.01)
(For anglerange=10 To .0001 Step -.0001)
or
(For anglerange=10 To .0001 Step -.0001)
(For anglerange=1 To .0001 Step -.0001)


This process is only a temporary solution until I can figure out how to actually calculate it.
calstover
Posts: 68
Joined: Aug 21, 2006 16:51

Post by calstover »

HD_
Posts: 215
Joined: Jun 10, 2006 12:15
Contact:

Post by HD_ »

An interesting trick when dealing with ellipses or ellipsoids is to go into ellipse space:
ellipse radii: (e_rx , e_ry , r_rz)
ellipse coord: (ex , ey , ez)
line coord: (lx , ly)

You could do special case testing for line ellipse or you can simplify it greatly by doing line-circle calculations on a new set of data:

new ellipse coord: (ex/e_rx , ey/e_ry , ez/e_rz)
new line coord: (lx/e_rx , ly/e_ry , lz/e_rz)

(just remove the z parts if you want only 2d).


You could probably change it around for the circle-ellipse test, and multiply the radii instead, and then do a circle circle test (if that simplifies things).
owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

circle ellipse intersection

Post by owen »

Sub calccircleellipseintersection is the same as two ellipses with the addition of a select case to figure out which object of the two is the circle and which is the ellipse.

in the case of a circle being the object, it's minor radius is set to that of the major radius (ie. both major and minor radii are set to that of the circle's radius)

currently these two subs are near the bottom of the source code.
Last edited by owen on Aug 01, 2013 14:36, edited 1 time in total.
owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

work around explanation

Post by owen »

my temp work around to calc intersections of ellipses and circles is:

1. first i determine if two objects are found near the mouse using Sub detectpoints() and Sub scanpoints(). these two routines are used to detect lines, circles, arcs, ellipses, and elliptical arcs.

2. upon finding two ellipses:
A. first i calc a rough angle, ie. the angle from the ellipse's center to the mouse, taking into consideration the rotation of the ellipse.
B. then i draw a line from (+-) 10 degrees of arc, meaning line(x1,y1) is a point on the ellipse, negative 10 degrees from that of the rough angle and (x2,y2) is a point on the ellipse, positive 10 degrees from that of the rough angle.
C. with two lines (or two segments), one for each ellipse, i then calc their intersection and use that point of reference as the angle instead of the mouse's angle.
D. with this new angle reference, the two segments are split half way near the actual intersection of the two ellipses as the for next loop reduces the arc coverage angles from (+-) 10 degrees down to (+-) .01 degrees.

note: starting off with (+-) 10 degrees was just arbitrary and may (will) cause errors in the case of two ellipses interesecting each other near their narrow ends.

again this is only a temporary solution but it works.

thanx community for sugestions, owen
owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

trim and extend functions

Post by owen »

currently working on TRIM function

currently have calculations in place which calc intersection(s) of a line
with other lines and circles (circles and arcs)

i am presenting a question to the fb community ???

as an example i present a given situation:
a line from (0,0) to (100,0)
ie. from (x1,y1) to (x2,y2)

lets say, hypotheticaly:
on this line i have 5 points (points of intersection) in an two arrays, ie.
pointsX(1 thru 5)
pointsY(1 thru 5)
for example:
point #1 equals ( pointsX(1), pointsY(1) )
point #2 equals ( pointsX(2), pointsY(2) )
etc...

point #1 x=20 y=0
point #2 x=10 y=0
point #3 x=30 y=0
point #4 x=50 y=0
point #5 x=70 y=0

i simple sort routine would be appropriate to sort the 5 points on the X axis in ascending order leaving us with:

point #1 x=10 y=0
point #2 x=20 y=0
point #3 x=30 y=0
point #4 x=50 y=0
point #5 x=70 y=0

with the points sorted in ascending order i can now identify the segments:
ie. segment #1 is from the 0,0 to 10,0
segment #2 is from 10,0 to 20,0
segment #3 is from 20 to 30
seg #4 is from 30 to 50
seg #5 is 50 to 70
seg #6 70 to 100

now here is the question???

if my line is slanted ie, (x1,y1)-(x2,y2) is (0,0)-(100,100)
and if have 5 random point (points on the line) - not necessarily in order

how can i go about sorting them in ascending order from the beginning of the line to the end of the line taking into consideration that the line can be @ ANY ANGLE?

thanx, owen
calstover
Posts: 68
Joined: Aug 21, 2006 16:51

Post by calstover »

You can check 4 cases, using X1,Y1 as start of the
line (not the points) and X2,Y2 as the end.


Case X1<=X2 and Y1<=Y2
Sort by X ascending. Use Y ascending as a tiebreaker

Case X1<=X2 and Y1>Y2
Sort by X ascending. Use Y descending as a tiebreaker

Case X1>X2 and Y1<=Y2
Sort by X descending. Use Y ascending as a tiebreaker

Case X1>X2 and Y1>Y2
Sort by X descending. Use Y descending as a tiebreaker
owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

TRIM function half way workin

Post by owen »

thanx for the 4 case solution with sorting.

currently the trim function detects points of intersection of a line with:
other lines, circles, arcs and elliptical arcs.

step 1: draw some obects (line, circles, etc) so that some of the lines intersect with other objects.
step 2: select all objects (use box selects - turns all objects purple)
step 3: enable T button (trim button)
step 4: placing the mouse over a line will draw small circles at the points of intersection of that line and other objects it intersects. Also, the particular segment of the line that the mouse is nearest will high light in green
step 5: clicking on a green line (high lighted segment) will TRIM the line accordingly.

step 6: disable T (trim) when done trimming

next goal:
to write the code that trims circles and arcs to: lines, other circles, other arcs, ellipses and elliptical arcs.
Post Reply