How To Draw A Water Lily Easy
Using LuaTeX, it is easy to generate some more generic results.
\documentclass{article} \usepackage{tikz} \usepackage{luacode} \begin{document} \tikzset{ arcstyle/.style={ thick } } \begin{luacode*} one_degree = math.pi / 180 function get_inscribed_point(radius, n_poly, index, rotation) local ang = (360.0 / n_poly * index + rotation) * one_degree local x = radius * math.cos(ang) local y = radius * math.sin(ang) return {x,y} end function get_arc_info(p1, p2, p3) local xa, ya = table.unpack(p1) local xb, yb = table.unpack(p2) local xc, yc = table.unpack(p3) local coef1 = xb*xb - xc*xc + yb*yb - yc*yc local coef2 = xa*xa - xb*xb + ya*ya - yb*yb local coef3 = 2.0 * ((xa-xb)*(yb-yc)-(xb-xc)*(ya-yb)) -- calculate center local center_x = (-(ya-yb)*coef1+(yb-yc)*coef2)/coef3 local center_y = ((xa-xb)*coef1-(xb-xc)*coef2)/coef3 -- calculate radius local radius = math.sqrt(math.pow(xa-center_x, 2)+math.pow(ya-center_y, 2)) -- calculate arc angle range local arc_ang1 = math.asin((0.5*math.sqrt(math.pow(xb-xa,2)+math.pow(yb-ya,2))/radius)) local arc_ang2 = math.asin((0.5*math.sqrt(math.pow(xc-xb,2)+math.pow(yc-yb,2))/radius)) local arc_angle = (arc_ang1 + arc_ang2) * 2.0 -- find out if (xc,yc) or (xa,ya) has the smallest angle -- make sure xc has the smallest angle (if not, swap two points) local ang_a = math.atan2(ya-center_y,xa-center_x) local ang_c = math.atan2(yc-center_y,xc-center_x) if ang_a < ang_c then ang_a = ang_a + 2 * math.pi end -- determine start angle local start_angle =ang_c local end_angle = ang_a -- return results return { ["center_x"] = center_x, ["center_y"] = center_y, ["radius"] = radius, ["start_angle"] = start_angle / one_degree, ["end_angle"] = end_angle / one_degree, ["arc_start_x"] = xc, ["arc_start_y"] = yc } end function draw_arc(p1, p2, p3) local arc = get_arc_info(p1, p2, p3) tex.print(string.format([[\draw[arcstyle] (%f cm, %f cm) arc (%f:%f:%f cm);]], arc["arc_start_x"], arc["arc_start_y"], arc["start_angle"], arc["end_angle"], arc["radius"])) end function draw_lily(radius, n_poly, offset, rotation) for i=1,n_poly do local ind1 = i - 1 local ind2 = (i+offset - 1)%n_poly local p1 = get_inscribed_point(radius, n_poly, ind1, rotation) local p2 = {0.0,0.0} local p3 = get_inscribed_point(radius, n_poly, ind2, rotation) draw_arc(p1, p2, p3) end end \end{luacode*} \begin{tikzpicture} \draw (0,0) circle (2cm); \directlua{ draw_lily(2.0, 6, 2, 0.0) } \end{tikzpicture} \begin{tikzpicture} \draw (0,0) circle (2cm); \directlua{ draw_lily(2.0, 6, 2, 15.0) } \end{tikzpicture} \begin{tikzpicture} \draw (0,0) circle (2cm); \directlua{ draw_lily(2.0, 8, 2, 15.0) } \end{tikzpicture} \begin{tikzpicture} \draw (0,0) circle (2cm); \directlua{ draw_lily(2.0, 12, 2, 15.0) } \end{tikzpicture} \begin{tikzpicture} \draw (0,0) circle (2cm); \directlua{ draw_lily(2.0, 12, 3, 15.0) } \end{tikzpicture} \begin{tikzpicture} \draw (0,0) circle (2cm); \directlua{ draw_lily(2.0, 12, 4, 15.0) } \end{tikzpicture} \begin{tikzpicture} \draw (0,0) circle (2cm); \directlua{ draw_lily(2.0, 36, 2, 15.0) } \end{tikzpicture} \end{document}
How To Draw A Water Lily Easy
Source: https://tex.stackexchange.com/questions/573574/how-can-i-draw-a-water-lily-in-latex
Posted by: compoorwastincer.blogspot.com
0 Response to "How To Draw A Water Lily Easy"
Post a Comment