3
$\begingroup$

I am an absolute beginner in Mathematica. For a small test project, I am supposed to change the following code such that the corners of the polygon {0, 0, 0}, {1, 1, 1}, {0, 1, 0}, {1, 0, 1} can be manipulated. I did not find any satisfactory solution so far.

Manipulate[
  Graphics3D[
    Polygon[{{0, 0, 0}, {1, 1, 1}, {0, 1, 0}, {1, 0, 1}}, 
      VertexColors -> {a, b, c, d}], 
    ImageSize -> {600, 600}], 
  {{a, Red, "first corner"}, Red}, 
  {{b, Green, "second corner"}, Green},
  {{c, Blue, "third corner"}, Blue}, 
  {{d, Yellow, "fourth corner"}, Yellow}]
$\endgroup$
1
  • $\begingroup$ What kind of manipulation to have in mind? What action should change invoke in the displayed polygon? Is it the position of the corners, or something else? $\endgroup$
    – m_goldberg
    Commented Apr 17, 2018 at 18:16

1 Answer 1

5
$\begingroup$

Depending on how you want to manipulate the corners, you could do it a few ways.

  1. Inputting points as a list

    Manipulate[
     Graphics3D[Polygon[{p1, p2, p3, p4}, VertexColors -> {a, b, c, d}], 
       ImageSize -> {600, 600}], {{a, Red, "first corner"}, 
      Red}, {{b, Green, "second corner"}, 
      Green}, {{c, Blue, "third corner"}, 
      Blue}, {{d, Yellow, "fourth corner"}, Yellow},
     Column[{Control[{{p1, {0, 0, 0}}}], Control[{{p2, {1, 1, 1}}}], 
       Control[{{p3, {0, 1, 0}}}], Control[{{p4, {1, 0, 1}}}]}]]
    

Output: User inputs the corners as a list

1b. Prettier version of 1:

Manipulate[
 Graphics3D[Polygon[{p1, p2, p3, p4}, VertexColors -> {a, b, c, d}], 
  ImageSize -> {600, 600}], {{a, Red, "first corner"}, 
  Red}, {{b, Green, "second corner"}, 
  Green}, {{c, Blue, "third corner"}, 
  Blue}, {{d, Yellow, "fourth corner"}, Yellow},
 Control[{{p1, {0, 0, 0}}}], Control[{{p2, {1, 1, 1}}}], 
 Control[{{p3, {0, 1, 0}}}], Control[{{p4, {1, 0, 1}}}]]

Output: Prettier version of 1

  1. User could use sliders to slide the corners

    Manipulate[ Graphics3D[ Polygon[{{p1x, p1y, p1z}, {p2x, p2y, p2z}, {p3x, p3y, p3z}, {p4x, p4y, p4z}}, VertexColors -> {a, b, c, d}], ImageSize -> {600, 600}], {{a, Red, "first corner"}, Red}, {{b, Green, "second corner"}, Green}, {{c, Blue, "third corner"}, Blue}, {{d, Yellow, "fourth corner"}, Yellow}, Grid[{ {Control[{p1x, 0, 1}], Control[{p1y, 0, 1}], Control[{p1z, 0, 1}]}, {Control[{p2x, 0, 1}], Control[{p2y, 0, 1}], Control[{p2z, 0, 1}]}, {Control[{p3x, 0, 1}], Control[{p3y, 0, 1}], Control[{p3z, 0, 1}]}, {Control[{p4x, 0, 1}], Control[{p4y, 0, 1}], Control[{p4z, 0, 1}]}}]]

Output: Sliders

$\endgroup$
2
  • 1
    $\begingroup$ thats exactly what I was looking for. Thank you! $\endgroup$
    – MARIUS
    Commented Apr 17, 2018 at 17:00
  • $\begingroup$ You're welcome! Make sure to mark this as the correct answer if it was sufficient!! $\endgroup$
    – Max Coplan
    Commented Apr 17, 2018 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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