0
$\begingroup$
parta = ContourPlot[
y == (Sqrt[Abs[x]] + Sqrt[1 - x^2]), {x, -2, 2}, {y, -2, 2}, 
PlotPoints -> 100, MaxRecursion -> 1];
partb = ContourPlot[
y == (Sqrt[Abs[x]] - Sqrt[1 - x^2]), {x, -2, 2}, {y, -2, 2}, 
PlotPoints -> 100, MaxRecursion -> 1];
heart = JoinedCurve[parta, partb];
Show[heart, PlotRange -> All] 

I want to join(merge) parta and partb to heart,then I can rotate or move only one object heart.But JoinedCurve doesn't join parta and partb.

How to solve this problem?

$\endgroup$
  • $\begingroup$ You have to take the == out of Piecewise. $\endgroup$ – JEM_Mosig Mar 8 '18 at 2:39
  • $\begingroup$ @JEM_Mosig,thanks for your reply!I updated the question,revised the codes,please check it. $\endgroup$ – kittygirl Mar 8 '18 at 3:43
  • 1
    $\begingroup$ You don't need JoinedCurve. Just evaluate Show[parta, partb] $\endgroup$ – m_goldberg Mar 8 '18 at 4:39
  • $\begingroup$ If I use Show[parta, partb],I need to rotate or move 2 parts.If I can merge parta and partb to heart,there's only one object I need to care. $\endgroup$ – kittygirl Mar 8 '18 at 5:36
2
$\begingroup$

This should do it:

ContourPlot[
    Piecewise[{
        {2*x^2 - 2*x*y + y^2, x > 0},
        {2*x^2 + 2*x*y + y^2, x <= 0}
    }] == 1, 
    {x, -1, 1}, {y, -1, 1.5}, 
    PlotPoints -> 100, 
    MaxRecursion -> 1
]

Addednum (on your revised question)

If you must use JoinedCurve, then you have to apply JoinedCurve to the graphics primitives, i.e. Line objects. This can be done as follows.

Evaluate the code in your question to define parta and partb. Note that ContourPlot returns a Graphics with a GraphicsComplex inside:

parta // InputForm // Shallow
(* Graphics[GraphicsComplex[<<2>>], {<<11>>}] *)

Then extract the graphics primitives and the coordinates from the GraphicsComplex and combine them:

With[{
    (* find the line object in the first figure *)
    aLine = FirstCase[parta, Line[___], Null, Infinity],
    (* extract the coordinates of the points of that line *)
    aCoords = FirstCase[parta, GraphicsComplex[coords_, ___] :> coords, Null, Infinity],
    (* find the line object in the second figure *)
    bLine = FirstCase[partb, Line[___], Null, Infinity],
    (* extract the coordinates of the points of that line *)
    bCoords = FirstCase[partb, GraphicsComplex[coords_, ___] :> coords, Null, Infinity]
  },
  Graphics[
    GraphicsComplex[
      Join[aCoords, bCoords],
      JoinedCurve[{
          {aLine},
          (* we have to shift the integers that represent the points,
            because the coordinates of the second line begin after the
            coordinates of the first line *)
          {bLine /. n_Integer :> n + Length[aCoords]}
        }, CurveClosed -> False
      ]
    ]
  ]
]

Of course, if you really just want to create the figure and are not interested in the graphics primitive of the curve, then your code almost works - just replace JoinedCurve by {}:

parta = ContourPlot[
  y == (Sqrt[Abs[x]] + Sqrt[1 - x^2]), {x, -2, 2}, {y, -2, 2}, 
  PlotPoints -> 100, MaxRecursion -> 1];
partb = ContourPlot[
  y == (Sqrt[Abs[x]] - Sqrt[1 - x^2]), {x, -2, 2}, {y, -2, 2}, 
  PlotPoints -> 100, MaxRecursion -> 1];

Show[{parta, partb}, PlotRange -> All]
$\endgroup$
  • $\begingroup$ Your answer works well.I am curious why mathematica has no function to do it. $\endgroup$ – kittygirl Mar 8 '18 at 4:29
  • $\begingroup$ @kittygirl: Normally you don't need the graphics primitive, but just the graphics itself. In that case you can use Show without JoinedCurve, as m_goldberg has suggested. $\endgroup$ – JEM_Mosig Mar 8 '18 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.