gltut / Documents / Basics / GenVectorNegation.lua

require "SvgWriter"
require "vmath"
require "Viewport"
require "SubImage"
require "GridAxis"
require "_utils"

local subImages = SubImage.SubImage(1, 1, 300, 300, 100, 0);

local coordSize = 10;

local vp = Viewport.Viewport({300, 300}, {0, 2}, coordSize)
local trans2 = Viewport.Transform2D()
vp:SetTransform(trans2);

local styleLib = SvgWriter.StyleLibrary();

local vectorColor = "black";
local vectorSumColor = "red";
local pointSize = 15;

styleLib:AddStyle(nil, "vector",
	SvgWriter.Style():stroke(vectorColor):marker_end(SvgWriter.uriLocalElement("arrow")));
styleLib:AddStyle(nil, "arrowhead",
	SvgWriter.Style():stroke(vectorColor):fill(vectorColor));
styleLib:AddStyle(nil, "text",
	SvgWriter.Style():stroke("none"):fill(vectorColor):font_weight("bold"):font_size("25px"):font_family("monospace") );
	
styleLib:AddStyle(nil, "vector-sum",
	SvgWriter.Style():stroke(vectorSumColor):marker_end(SvgWriter.uriLocalElement("arrow-sum")));
styleLib:AddStyle(nil, "arrowhead-sum",
	SvgWriter.Style():stroke(vectorSumColor):fill(vectorSumColor));
styleLib:AddStyle(nil, "text-sum",
	SvgWriter.Style():stroke("none"):fill(vectorSumColor):font_weight("bold"):font_size("25px"):font_family("monospace"):text_anchor("end") );
	
local axisData = GridAxis.GridAxis2D(vp, nil, styleLib, true, nil);

local vectors =
{
	vmath.vec2(0, 0),
	vmath.vec2(3, 4),

	vmath.vec2(0, 4),
	vmath.vec2(-3, 0),
};

local labelOffset = vmath.vec2(5, -12);

local vectorLabels = 
{
	"A", vmath.vec2(10, 10),
	"-A", vmath.vec2(-5, -5),
}

local styles =
{
	{{"vector"}, {"text"}}, 
	{{"vector-sum"}, {"text-sum"}}, 
};

vectors = vp:Transform(vectors);

local writer = SvgWriter.SvgWriter(ConstructSVGName(arg[0]), {subImages:Size().x .."px", subImages:Size().y .. "px"});
	writer:StyleLibrary(styleLib);
	axisData:AddDefinitions(writer, "g_axes");
	writer:BeginDefinitions();
		WriteStandardArrowhead(writer, "arrow", {"arrowhead"});
		WriteStandardArrowhead(writer, "arrow-sum", {"arrowhead-sum"});
	writer:EndDefinitions();

	writer:Use("g_axes", subImages:Offset(1, 1), subImages:SubSize());

	for i=1, #vectors, 2 do
		writer:Line(vectors[i], vectors[i + 1], styles[(i+1)/2][1]);
		writer:Text(vectorLabels[i],
			((vectors[i] + vectors[i + 1]) / 2) + vectorLabels[i + 1], styles[(i+1)/2][2]);
	end
writer:Close();
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.