I have a task in my university to make a line made out of 3 points by modifying the 2-point-line I already have. could anyone help me?
the 2-point line is:
classdef MgGeoLine2Point < mgen.MgGeoLine
% A 2-point line of the geometry
properties (Access = private)
geoPoints = mgen.MgGeoPoint.empty(0,2);
gridNodes = mgen.MgGridNode.empty(0,0);
numTargetGridNodes = 0;
switchGridNode = false;
switchGeoPoint = false;
normFunction = mgen.MgNormFunction();
end
%
methods
%%constructor
function obj = MgGeoLine2Point(geoPoints, numNodes, normFunction)
if nargin >= 2
obj.geoPoints = geoPoints;
obj.numTargetGridNodes = numNodes;
obj.normFunction = mgen.MgNormFunction( mgen.MgNormFunctionType.Linear );
end
if nargin >= 3
obj.normFunction = normFunction;
end
end
%%print data
function str = print(self)
str = sprintf( 'MgGeoLine2Point: gn1.id = %2d, gn2.id = %2d\n', self.gridNodes(1).id, self.gridNodes(end).id );
%
for gn = self.gridNodes
str = [ str, gn.print(), '\n' ];
end
end
%%---------------------------------------------------------------------
function p = getGeoPoint(self,id)
if self.switchGeoPoint
p = self.geoPoints(3-id);
else
p = self.geoPoints(id);
end
end
function g = getGridNode(self, id)
if ~self.switchGridNode
g = self.gridNodes(id);
else
g = self.gridNodes(end-id+1);
end
end
function setNumTargetGridNodes(self, num)
self.numTargetGridNodes = num;
end
function num = getNumTargetGridNodes(self)
num = self.numTargetGridNodes;
end
function switchStartGridNode(self)
self.switchGridNode = ~self.switchGridNode;
end
function switchStartGeoPoint(self)
self.switchGeoPoint = ~self.switchGeoPoint;
end
function resetStartGridNode(self)
self.switchGridNode = false;
end
function resetStartGeoPoint(self)
self.switchGeoPoint = false;
end
%
function g = getCommonGridNode(self, geoline)
%
if self.getGridNode(1).id ~= geoline.getGridNode(1).id
geoline.switchStartGridNode();
end
if self.getGridNode(1).id ~= geoline.getGridNode(1).id
self.switchStartGridNode();
end
if self.getGridNode(1).id ~= geoline.getGridNode(1).id
geoline.switchStartGridNode();
end
if self.getGridNode(1).id ~= geoline.getGridNode(1).id
disp('MgGeoLine2Point:getCommonGridNode() --> GridNodes not matching!')
end
g = self.getGridNode(1);
end
%
function g = getCommonGeoPoint(self, geoline)
if self.getGeoPoint(1) ~= geoline.getGeoPoint(1)
geoline.switchStartGeoPoint();
end
if self.getGeoPoint(1) ~= geoline.getGeoPoint(1)
self.switchStartGeoPoint();
end
if self.getGeoPoint(1) ~= geoline.getGeoPoint(1)
geoline.switchStartGeoPoint();
end
if self.getGeoPoint(1) ~= geoline.getGeoPoint(1)
disp('MgGeoLine2Point:getCommonGeoPoint() --> GeoPoints not matching!')
end
g = self.getGeoPoint(1);
end
%
function alignLines(self, geoline)
self.getCommonGeoPoint(geoline);
self.switchStartGeoPoint();
%
if length(self.gridNodes) > 0
self.getCommonGridNode(geoline);
self.switchStartGridNode();
end
end
%
function gnv = makeGridNodes(self, gridnodes)
%
gnv = gridnodes;
%
if length(self.gridNodes) > 0
return;
end
%
run_id = 1+length(gridnodes);
dim = self.numTargetGridNodes;
segments = dim-1;
x0 = self.geoPoints(1).coord(1);
x1 = self.geoPoints(1).coord(2);
lx0 = self.geoPoints(2).coord(1) - x0;
lx1 = self.geoPoints(2).coord(2) - x1;
%
% occupy vector of GridNodes with the existing ones
self.gridNodes = mgen.MgGridNode.empty(0,dim);
self.gridNodes(1) = self.geoPoints(1).gnode;
self.gridNodes(dim) = self.geoPoints(2).gnode;
%
% set function values
self.normFunction.vals(1) = segments;
if self.normFunction.vals(2) == 0
self.normFunction.vals(1) = segments;
end
if self.normFunction.vals(1) == 1
self.normFunction.vals(2) = segments;
end
%
% generate new GridNodes
for ii = 1:segments-1
scale = self.normFunction.eval( ii/segments );
self.gridNodes(ii+1) = mgen.MgGridNode( run_id, [x0+scale*lx0, x1+scale*lx1] );
gnv(end+1) = self.gridNodes(ii+1);
run_id = run_id + 1;
end
end
function gp = getGeoPoints(self)
gp = self.geoPoints;
end
function gn = getGridNodes(self)
gn = self.gridNodes;
end
end
end
Anybody could help me a little bit?

2 Comments

Hi,
Please provide a description of the exact help that you are looking for. Are you facing any errors or do you need help in doubts regarding the code. providing a detailed description will let the community provide you with better answers.
Regards Vineeth
I don't understand the code properly, the class above, gets 2 points of geometry and draws a straight line between them. My task is to write a code that draws a quadratic curve between three points "geoPoint". I already have a function that does that but I don't know how and where to implement it
function cfun(A,B,C,N)
M = [1 0 0 0 0 0; 0 1 0 0 0 0; 1 0 0.5 0 0.25 0; 0 1 0 0.5 0 0.25; 1 0 1 0 1 0; 0 1 0 1 0 1];
b = [A; B; C];
x = M\b;
a0 = x(1:2);
a1 = x(3:4);
a2 = x(5:6);
clf; hold on;
plot([A(1) B(1) C(1)], [A(2), B(2), C(2)], 'ro');
t = linspace(0,1,100);
x = a2 * t.^2 + a1 * t + a0 * ones(1,length(t));
plot(x(1,:), x(2,:), 'b-');
t = linspace(0,1,N);
x = a2 * t.^2 + a1 * t + a0 * ones(1,length(t));
plot(x(1,:), x(2,:), 'kx');
end

Sign in to comment.

 Accepted Answer

Guillaume
Guillaume on 18 May 2016
Edited: Guillaume on 18 May 2016
" the class above, gets 2 points of geometry and draws a straight line".
The class you've posted does no drawing whatsoever. And despite its name also does not appear to restrict itself to storing only two points. The constructor certainly does not appear to restrict the numbers of points stored. It looks like the only function that sort of assumes two points is the debugging function print (it prints only two points, the first and the last) but it also looks like getGeoPoint assumes 3 points.
In fact, it looks like the methods implemented in the class actually belong to the base class. Are they just a copy of the functions in MgGeoLine? To me it looks like somebody either didn't understand inheritance or completely misnamed their classes.
It's near impossible to answer your question in any case as we don't know anything about the base class from which MgGeoLine2Point derives. It certainly does not restrict itself to storing points, since there's also something about a norm function and something about creating a grid.

3 Comments

I have uploaded the whole framework if you want to take a look at it!
I've had a brief look. This looks like a work in progress that's not been tested properly. There are exactly 4 differences between MGeoLine2Point and MgeoLine3Point:
  1. The private attribute of the properties has been commented out in MgeoLine3Point. This has no effect on behaviour.
  2. The 'starting value' of the properties are slightly different. Theses values will never be used anyway since they get replaced in the constructor, so no effect.
  3. The check for the number of inputs has been increased by one in MgeoLine3Point. This is indication that whoever wrote the code don't really know what they're doing. The number of declared inputs hasn't changed so the check does not work anymore. A bug.
  4. the print method prints a different class name. No change otherwise.
That's it. There's no functional difference between the two classes. Apart from the bug in MgeoLine3Point, you can use one instead of the other with no difference in behaviour.
Therefore, I would recommend you go back to whoever wrote this code and ask for clarification and to provide documentation on how to use the classes.
I'll do that, Thank you very much.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!