TransWikia.com

Multiple replacement/variables

Mathematica Asked on February 19, 2021

In the, somewhat basic, code below I would like to learn how to replace the g2, g5 and g8 lines with a shorter command. I’ve made use of one parameter #1 but how do I make use of a second to vary the .2, .5, and .8 values? Perhaps there are other modifications to the code that you suggest to make it better and more ”general” and flexible. TIA.

L = 10;
y[x_] := Sqrt[L^2 - x^2]
gr1 = Graphics[Line[{{10 - #1, 0}, {0, y[10 - #1]}}] & /@ Range[0, 10, .125]];
p2 = {10 - #1, 0} + ({0, y[10 - #1]} - {10 - #1, 0})*.2 & /@ Range[0, 10, .125];
p5 = {10 - #1, 0} + ({0, y[10 - #1]} - {10 - #1, 0})*.5 & /@ Range[0, 10, .125];
p8 = {10 - #1, 0} + ({0, y[10 - #1]} - {10 - #1, 0})*.8 & /@ Range[0, 10, .125];
gr2 = Graphics[{Red, Point[p2]}];
gr5 = Graphics[{Red, Point[p5]}];
gr8 = Graphics[{Red, Point[p8]}];
Show[gr1, gr2, gr5, gr8]

3 Answers

linecoords = {{#, 0}, {0, y[#]}} & /@ (10 - Range[0, 10, .125]);
pointcoords = Table[lc[[1]] (1 - i) + i lc[[2]], {lc, linecoords}, {i, {.2, .5, .8}}];

Graphics[{Line[linecoords], Red, Point /@ pointcoords}]

enter image description here

Correct answer by kglr on February 19, 2021

This is a good use case for a Table:

{p2, p5, p8} = 
   Table[
     {10 - a, 0} + m({0, y[10 - a]} - {10 - a, 0}),
     {m, {0.2, 0.5, 0.8}},
     {a, 0, 10, 0.125}
   ]

Answered by MarcoB on February 19, 2021

L = 10;
y[x_] := Sqrt[L^2 - x^2];
Graphics[{Table[
   Line[{{10 - i, 0}, {0, y[10 - i]}}], {i, 0, 10, .125}], Red, 
  Table[Point[{10 - i, 0} + ({0, y[10 - i]} - {10 - i, 0})*j], {i, 0, 
    10, .125}, {j, {.2, .5, .8}}]}]

enter image description here

Answered by cvgmt on February 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP