Modul:Mathematikunterricht/ Vorlage:Bruch in Kreisdarstellung

Aus Wikibooks

Informationen zu dieser Dokumentation
Modul Mathematikunterricht

Modulbeschreibung

Modul zur Darstellung von Brüchen als farbigen Teil eines Kreisdiagramms (Torte) für die Vorlage Mathematikunterricht/ Vorlage:Bruch in Kreisdarstellung. Die Vorlage benutzt die Funktion kreisdarstellungWrapper.

Beispiel Vorlagenbenutzung für den Bruch 22/8:

{{:Mathematikunterricht/ Vorlage:Bruch in Kreisdarstellung |18 |8 |durchmesser= 60
|farbe1= orange |farbeRest= greenyellow}}

Ergebnis:



Ergebnis wie oben, mit |blumenmuster= 3:



Parameter:

  • 1, 2 für Zähler, Nenner
  • durchmesser (für Vorlage:Graph:Chart)
  • direction Ausmalrichtung: ccw (Gegenuhrzeigersinn, Standardwert) oder cw
  • farbe1, farbeRest für die Farbe.., styleDiv für den div-Box Style
  • blumenmuster 1, 2 oder 3 für eine gemusterte Darstellung

Funktionen

ggT

greatest common divisor (with prime factors <=23)
Use from a module also like util= require("Modul:.."); T= util.ggT(24,36)

example: ggT(24, 36) --result: 12

kreisdarstellung

Hauptfunktion

kreisdarstellungWrapper

Wrapperfunktion, kann aus der Vorlage ohne Parameter aufgerufen werden


Information


--- Modul zur Darstellung von Brüchen als farbigen Teil eines Kreisdiagramms
-- (Torte) für die Vorlage [[Mathematikunterricht/ Vorlage:Bruch in Kreisdarstellung]].
-- Die Vorlage benutzt die Funktion <code>kreisdarstellungWrapper</code>.<br><br>
-- Beispiel Vorlagenbenutzung für den Bruch 22/8:<br>
--  {<nowiki/>{:Mathematikunterricht/ Vorlage:Bruch in Kreisdarstellung |18 |8 |durchmesser= 60
--  |farbe1= orange |farbeRest= greenyellow}}
-- Ergebnis:<br>
-- {{:Mathematikunterricht/ Vorlage:Bruch in Kreisdarstellung |18 |8 |durchmesser= 60
-- |farbe1= orange |farbeRest= greenyellow}}<br>
-- <br>
-- Ergebnis wie oben, mit <code>|blumenmuster= 3</code>:<br>
-- {{:Mathematikunterricht/ Vorlage:Bruch in Kreisdarstellung |18 |8 |durchmesser= 60
-- |farbe1= orange |farbeRest= greenyellow |blumenmuster= 3}}<br>
-- <br>
-- Parameter: 
-- * <code>1</code>, <code>2</code> für Zähler, Nenner
-- * <code>durchmesser</code> (für [[Vorlage:Graph:Chart]])
-- * <code>direction</code> Ausmalrichtung: <code>ccw</code> (Gegenuhrzeigersinn, Standardwert) oder <code>cw</code> 
-- * <code>farbe1</code>, <code>farbeRest</code> für die Farbe.., <code>styleDiv</code> für den div-Box Style
-- * <code>blumenmuster</code> 1, 2 oder 3 für eine gemusterte Darstellung
local p = {}


function makeSimpleColors(nom, denom, color1, colorRest)
	o= ""
	for i= 1, denom do
		if i~=1 then 
			o= o..","
		end
		iref= denom-i+1
		if cw then iref= i end
		if iref<=nom then
			o= o..color1
		else
			o= o..colorRest
		end
	end
	--mw.addWarning( "simpl " )
	return o
end

gprimes= {2,3,5,7,11,13,17,19,23}
--- greatest common divisor (with prime factors <=23)<br>
-- Use from a module also like util= require("Modul:.."); T= util.ggT(24,36)
-- example: ggT(24, 36) --result: 12
function p.ggT(a, b) 
	facs= {}
	for j= 1, 20 do
		for i= 1, #gprimes do
		    if a%gprimes[i]==0 and b%gprimes[i]==0 then
		      facs[#facs+1]= gprimes[i]
		      a= a/gprimes[i]; b= b/gprimes[i] 
		      break
		    end
		end
	end
	ggt= 1
	for i= 1, #facs do
		ggt= ggt* facs[i]
	end
	return ggt
end

function makeFlowerColors(n, d, color1, colorRest)
	cs= {}; ndone= 0
	for i= 1, d do
		cs[i]= colorRest
	end
	T= p.ggT(n, d) 
	--mw.addWarning( "flower0ndT "..n..d.." "..T)
	if T>1 then --3/6, 6/8..
		for i= 1, T do
			for j= 1, d/T do
				if j<=n/T then
					cs[(i-1)*(d/T)+j]= color1 
				end
			end
		end
		--mw.addWarning( "flower1 "..n..d..mw.dumpObject(cs))
	elseif d%2==1 then --2/3, 2/5..
		for i= 1, d do
			if i==1 then --1 ->4
				if n%2==1 then
					cs[(d+1)/2]= color1
					ndone= ndone+1
				end
			elseif ndone<n then --2,4,6 ->1,2,3
				if i%2==0 then
					cs[i/2]= color1
				else --3,5,7 ->7,6,5
					cs[d-(i-3)/2]= color1
				end
				ndone= ndone+1
			end
		end
		--mw.addWarning( "flower2 "..n..d..mw.dumpObject(cs))
	else
		t= 1
		if p.ggT(d,2)==1 then t= 2
		elseif p.ggT(d,3)==1 then t=3 
		end
		for i= 1, n do
			cs[((i-1)*t)%d+1]= color1
		end
		--mw.addWarning( "flower3 "..n..d..mw.dumpObject(cs))
	end
	
	return table.concat(cs, ", ")
end
	

	
--- Hauptfunktion
function p.kreisdarstellung(frame)
	nom= assert(tonumber(frame.args[1]), "nonumber arg1-2 "..mw.dumpObject(frame.args))
	denom= assert(tonumber(frame.args[2]), "nonumber arg1-2 "..mw.dumpObject(frame.args))
	diameter= assert(tonumber(frame.args["durchmesser"] or 40), "nonumber durchmesser "..mw.dumpObject(frame.args))
	cw= frame.args["direction"] and frame.args["direction"]~="ccw" 
	color1= frame.args["farbe1"] or "#88d"
	colorRest= frame.args["farbeRest"] or "#eed"
	styleDiv= frame.args["styleDiv"] or ""
	flower= assert(tonumber(frame.args["blumenmuster"] or 0), "nonumber blumenmuster")

	
	--setup strings
	--for Graph:Chart|width=60|height=60|type=pie  |x=1,2,3,4,5,6,7  |y=1,1,1,1,1,1,1  |colors=#eee,green,#eee,#eee,green,#eee,#eee
	nomprop= nom - math.floor(nom/denom)*denom
	den1234= ""; ones= ""; colorsfull= ""
	for i= 1, denom do
		if i~=1 then 
			den1234= den1234..","; ones= ones.."," 
			colorsfull= colorsfull..","
		end
		den1234= den1234..i
		ones= ones.."1"
		iref= denom-i+1
		colorsfull= colorsfull..color1
	end
	--mw.addWarning( "setupstrings "..den1234..", "..ones..", "..colorsfull )

	colors= {};
	--flower pattern, like each second segment colored
	if flower>0 then
		noms= {}
		--flowerify more than one fraction?
		if flower>=2 and nom/denom>1 and nom/denom<=2 and nom%2==0 then
			noms[1]= nom/2; noms[2]=nom/2
		elseif flower>=3 and nom/denom>2 and nom/denom<=3 and nom%3==0 then
			noms[1]= nom/3; noms[2]=nom/3; noms[3]=nom/3
		else
			for fri= 1, math.ceil(nom/denom) do
				if fri~= math.ceil(nom/denom) then 
					noms[fri]= denom
				else
					noms[fri]= nomprop
				end
			end
		end
		for fri= 1, #noms do
			colors[fri]= makeFlowerColors(noms[fri], denom, color1, colorRest)
		end
		--mw.addWarning( "flowercolors "..mw.dumpObject(colors) )
		
	--simple, no pattern
	else
		for fri= 1, math.ceil(nom/denom) do
			if fri~= math.ceil(nom/denom) then 
				colors[fri]= colorsfull
			else
				colors[fri]= makeSimpleColors(nomprop, denom, color1, colorRest)
			end
		end
		--mw.addWarning( "simplecolors "..mw.dumpObject(colors) )
	end
	
	out= ""
	for fri= 1, #colors do
		out= out..frame:expandTemplate{ title = 'Graph:Chart', args = {
			width=diameter, height=diameter, type="pie", 
			x=den1234, y=ones, colors= colors[fri]} }
	end
	out= '<div style="display:inline-block;'..styleDiv..'">'..out..'</div>'
	return out
end

--- Wrapperfunktion, kann aus der Vorlage ohne Parameter aufgerufen werden
function p.kreisdarstellungWrapper(frame)
	return p.kreisdarstellung(frame:getParent())
end

return p