# How can I draw a line and an ellipse in the same coordinate system?

In this way, the x==y+n straight line and the x ^ 2/a ^ 2+y ^ 2/b ^ 2==1 elliptic image can be drawn to the same coordinate system.

For example：

x^2/16 + y^2/9 == 1, x == 2 y + 1


Specific to the above ellipse and straight line

ClearAll["*"]
eqns = {x^2/16 + y^2/9 == 1, x == 2 y + 1};
line = eqns[]
ell = eqns[]

pts = SolveValues[{line, ell}, {x, y}];
normalized = First[ell] - Last[ell];
(*ass=ResourceFunction["EllipseProperties"][ell,{x,y}];
params={a->ass["SemimajorAxisLength"],b->ass["SemiminorAxisLength"]}*)
params = {a -> Sqrt[Denominator[Coefficient[normalized, x^2]]],
b -> Sqrt[Denominator[Coefficient[normalized, y^2]]]}
glin = line[] /. params
gell = b {-1, 1} Sqrt[1 - x^2/a^2] /. params;
gpts = pts /. params;
(*Plot[{{glin,gell}},{x,-a,a}/. \
params,Epilog->{Red,PointSize[0.02],Point[gpts]}]*)

ParametricPlot[{glin, y}, {y, -b - 0.5, b + 0.5} /. params,
AxesLabel -> {x, y}]


The above code does not draw an ellipse and a straight line in the same coordinate system

##### 1. This is how to draw a single line, but the system will give an error prompt when replacing the value of b:

ParametricPlot::plln: Limiting value -0.5-b in {y,-0.5-b,0.5 +b} is not a machine-sized real number.

Reference resources: An answer by Daniel Huber

ParametricPlot[{glin, y}, {y, -b - 0.5, b + 0.5},
AxesLabel -> {x, y}] /. params


The function of line range expressed by b is that the range of line y value is consistent with the range of ellipse minor axis

##### 2. The code for drawing the intersection of a single ellipse image and a straight line is:
Plot[{gell}, {x, -a, a} /. params,
Epilog -> {Red, PointSize[0.02], Point[gpts]}]


There isn't any error in replacing a value here. Why?

Reference resources: Comments by Alexei Boulbitch

##### 3. Now, how can I integrate the line and ellipse images into a coordinate system?

The range of coordinates is automatically adjusted according to the value a and b of the major and minor axes of the ellipse. That's why the above replacement is used.

#### Code Update 1

ClearAll["*"]
eqs = {x^2/16 + y^2/9 == 1, x == 2 y + 1};
line = eqs[]
ell = eqs[]
pts = SolveValues[{line, ell}, {x, y}];
normalized = First[ell] - Last[ell];
ax = Sqrt[Denominator[Coefficient[normalized, x^2]]]
bx = Sqrt[Denominator[Coefficient[normalized, y^2]]]
(*ass=ResourceFunction["EllipseProperties"][ell,{x,y}];
params={a->ass["SemimajorAxisLength"],b->ass["SemiminorAxisLength"]}*)\
params = {a -> Sqrt[Denominator[Coefficient[normalized, x^2]]],
b -> Sqrt[Denominator[Coefficient[normalized, y^2]]]}
glin = line[] /. params
gell = b {-1, 1} Sqrt[1 - x^2/a^2] /. params;
gpts = pts /. params;
(*Hold@ContourPlot[Evaluate@{eqs},{x,-a-1,a+1},{y,-b-0.5,b+0.5},\
PlotLegends->Placed[eqs,{0.8,0.15}],AspectRatio->Automatic,Frame->\
params//ReleaseHold*)
ContourPlot[
Evaluate@{eqs}, {x, -ax - 1, ax + 1}, {y, -bx - 0.5, bx + 0.5},
Epilog -> {Red, PointSize[0.02], Point[gpts]},
PlotLegends -> Placed[eqs, {0.8, 0.15}], AspectRatio -> Automatic,
Frame -> False, Axes -> True, AxesStyle -> Arrowheads[{0.0, 0.04}],
AxesLabel -> {x, y}]


#### Code Update 2

Clear["Global*"]
eqs = {x^2/4 + y^2/3 == 1, y == 2 x + 1};
line = eqs[]
ell = eqs[]
pts = SolveValues[{line, ell}, {x, y}];
normalized = First[ell] - Last[ell];
ax = Sqrt[Denominator[Coefficient[normalized, x^2]]]
bx = Sqrt[Denominator[Coefficient[normalized, y^2]]]
p = Plot[y /. Solve[line, y], {x, -ax - 0.5, ax + 0.5}];
pts = SolveValues[{line, ell}, {x, y}]
Graphics[{{First@p}, {Red, Circle[{0, 0}, {ax, bx}],
Point[{0, 0}]}, {Blue, PointSize[.03], Point[pts]}}, Axes -> True,
AxesLabel -> {x, y}, AxesStyle -> Arrowheads[{0.0, 0.04}],
AspectRatio -> 1]
plx = Apply[Subtract, eqs, {1}];
pls = Numerator[Together[Apply[Subtract, eqs, {1}]]];
xpl = Collect[Resultant[pls[], pls[], y], x];
Collect[Coefficient[xpl, x^2] x^2 +
Factor@FactorTerms[Coefficient[xpl, x], x] x +
Select[xpl, FreeQ[x]], x, # &, Defer[+##]~Reverse~2 &] == 0
Collect[xpl, x, Simplify];
pl = {% == 0}
discx = Factor[Discriminant[xpl, x]]   (*discriminant*)
frist = Solve[eqs, {x, y}] // FullSimplify;
{{x1, y1}, {x2, y2}} = {x, y} /. frist;
second = {x1 + x2, x1 x2, y1 + y2, y1 y2,
y1 y2/(x1 x2), (x1 + x2)/2, (y1 + y2)/2} // FullSimplify
thrid = {x1 x2 + y1 y2, x1 y2 + x2 y1} // FullSimplify
slope = CoefficientList[line[], x][];    (*k*)
intercept = CoefficientList[line[], x][];  (*m*)
Chordlength =
FullSimplify[
Sqrt[1 + slope^2] Sqrt[(x1 + x2)^2 - 4 x1 x2]]    (*AbsAB*)
area = 1/2 Chordlength Sqrt[intercept^2]/Sqrt[slope^2 + 1] //
FullSimplify

• ContourPlot[Evaluate@{x^2/16 + y^2/9 == 1, x == 2 y + 1}, {x, -5, 5}, {y, -5, 5}, PlotLegends -> Placed[eqns, {0.8, 0.15}]] Jan 28 at 1:41
• thank you! I want to input the elliptic equation and the linear equation at the beginning, and then I will not input the elliptic equation and the linear equation for drawing and calculation, but directly use variables to extract. Includes the coordinate system x-axis y-axis range. It is better to draw the coordinate axis of the coordinate system. I have updated the problem. Jan 28 at 1:49
• ContourPlot[ Evaluate@{x^2/16 + y^2/9 == 1, x == 2 y + 1}, {x, -5, 5}, {y, -5, 5}, PlotLegends -> Placed[eqns, {0.8, 0.15}], Axes -> True, AxesLabel -> {x, y}] Jan 28 at 2:17

can be drawn to the same coordinate system.

Why not just use Graphics? Note that the Circle command can also draw an ellipse!  ClearAll["Global*"];
a = 4;
b = 3
centerAt = {0, 0}
lineEq = x == 2 y + 1
p = Plot[y /. Solve[lineEq, y], {x, -a, a}];
Graphics[{{Dashed,First@p},{Red,Circle[centerAt,{a, b}], Point[centerAt]}},Axes->True]


How to add an arrow to the coordinate axis?

You can use AxesStyle

Graphics[{{Dashed, First@p},
{Red, Circle[centerAt, {a, b}], Point[centerAt]}
},
Axes -> True, AxesStyle -> Arrowheads[{0.0, 0.04}]] How to show the intersection of line and ellipse

You can do similar to what Bob did which is use SolveValues and then use the Point command in the Graphics. Like this

ClearAll["Global*"];
a = 4;
b = 3
centerAt = {0, 0}
lineEq = x == 2 y + 1
elipseEq = x^2/a^2 + y^2/b^2 == 1;
p = Plot[y /. Solve[lineEq, y], {x, -a, a}];
pts = SolveValues[{lineEq, elipseEq}, {x, y}]
Graphics[{
{Dashed, First@p},
{Red, Circle[centerAt, {a, b}], Point[centerAt]},
{Blue, PointSize[.03], Point[pts]}
},
Axes -> True, AxesStyle -> Arrowheads[{0.0, 0.04}]] You can change the color, point size, etc.. as you want.

How to label the corresponding curve with the corresponding equation in the image

One way could be to use Text command and place the equations where you want them ClearAll["Global*"];
a = 4;
b = 3
centerAt = {0, 0}
lineEq = x == 2 y + 1
elipseEq = x^2/a^2 + y^2/b^2 == 1;
p = Plot[y /. Solve[lineEq, y], {x, -a, a}];
pts = SolveValues[{lineEq, elipseEq}, {x, y}]
Graphics[{
{Dashed, First@p},
{Red, Circle[centerAt, {a, b}], Point[centerAt]},
{Blue, PointSize[.03], Point[pts]},
{Rotate[Text[lineEq, {2, .8}], 30 Degree],
Text[elipseEq, {1.4, 3.4}]}
},
Axes -> True, AxesStyle -> Arrowheads[{0.0, 0.04}]
]

• Circle command can also draw an ellipse.I remember this usage. thank you! How to add an arrow to the coordinate axis? Jan 28 at 2:47
• @csn899 added arrows Jan 28 at 2:55
• Thank you very much! This method can draw an ellipse! It's amazing！Graphics[Circle[{0, 0}, {4, 3}], Axes -> True, AxesLabel -> {x, y}] Jan 28 at 2:58
• How to show the intersection of line and ellipse in the image. For example, the red dot represents the intersection of the two. Jan 28 at 3:41
• @csn899 updated for intersection Jan 28 at 3:50
Clear["Global*"]

eqns = {x^2/16 + y^2/9 == 1, x == 2 y + 1};


EDIT: Intersections are at

pts = SolveValues[eqns, {x, y}]

(* {{2/13 (2 - 3 Sqrt), 3/26 (-3 - 2 Sqrt)},
{2/13 (2 + 3 Sqrt), 3/26 (-3 + 2 Sqrt)}} *)

rng[var_] :=
Insert[(#[{var, eqns[]}, {x, y}] & /@
{MinValue, MaxValue}), var, 1];

Module[{xrng = rng[x], yrng = rng[y]},
Show[
ContourPlot @@ {eqns, xrng, yrng,
AspectRatio ->
(Subtract @@ Rest@yrng)/(Subtract @@ Rest@xrng),
Frame -> False,
Axes -> True,
AxesLabel -> Automatic},
Graphics[{Thin,
Arrow[{1.1 #, 0} & /@ (Rest@xrng)],
Arrow[{0, 1.13 #} & /@ (Rest@yrng)],
Red, AbsolutePointSize,
Tooltip[Point[#], N@#] & /@ pts}]]] • Thank you very much! A small question, how to add arrows to two coordinate axes? Jan 28 at 2:56
• Thank you very much! Jan 28 at 3:10

Using Region functionality:

Clear[x, y, r1, r2]
r1 = ImplicitRegion[x^2/16 + y^2/9 == 1, {x, y}];
r2 = ImplicitRegion[x == 2 y + 1, {{x, -4, 4}, y}];

Show[{Region[r1], Region[r2]
, Region[Style[
BooleanRegion[And, {r1, r2}]
, AbsolutePointSize, Red]
]
}
, PlotRange -> {{-4.5, 4.5}, {-4, 4}}
, AspectRatio -> Automatic
, Frame -> False
, Axes -> True
, AxesLabel -> {x, y}
, AxesStyle -> Arrowheads[{-0.04, 0.04}, {-0.04, 0.04}]
] ContourPlot[
Evaluate@{eqns}, {x, -a - 1, a + 1}, {y, -b - 0.5, b + 0.5},
PlotLegends -> Placed[eqns, {0.8, 0.15}], AspectRatio -> Automatic,
Axes -> True, AxesLabel -> {x, y}] /. params


This code can show that lines and ellipses are in the same coordinate system, but why does this error prompt still appear

ContourPlot::plln: Limiting value -1-a in {x,-1-a,1+a} is not a machine-sized real number.

ContourPlot[
Evaluate@{eqns}, {x, -a - 1, a + 1}, {y, -b - 0.5, b + 0.5},
PlotLegends -> Placed[eqns, {0.8, 0.15}], AspectRatio -> Automatic,
Axes -> True, AxesLabel -> {x, y}, Frame -> False] /. params

• It's a matter of evaluation order. If you don't want to see it: Hold@ContourPlot[ Evaluate@{eqns}, {x, -a - 1, a + 1}, {y, -b - 0.5, b + 0.5}, PlotLegends -> Placed[eqns, {0.8, 0.15}], AspectRatio -> Automatic, Axes -> True, AxesLabel -> {x, y}] /. params//ReleaseHold` Jan 28 at 3:51
• How to modify this code can be shown to the intersection of line and ellipse in the image. For example, the red dot represents the intersection of the two. Jan 28 at 5:58