Solved

Scale polygon by vertex

Posted on 2008-10-14
10
1,010 Views
Last Modified: 2013-11-20
I am working with some code that calcs area of a polygon by summing the vertexes, etc.. with a mathematical formula. How can I rescale this polygon when I have vertex coordinates to work with?
0
Comment
Question by:steenpat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
10 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 22715229
new = vertex + scale*(old - vertex)
0
 

Author Comment

by:steenpat
ID: 22716018
By old are you meaning the last vertex before this one? I tried this formula and its not working..
0
 

Author Comment

by:steenpat
ID: 22716332
let me rephrase what i am doing..
i guess i am not scaling the entire thing by one factor, I am rounding each segment to the nearest round quantity. like if a side is 5'7" it rounds to 6'0"
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:steenpat
ID: 22716702
i guess i should say this works for scaling by a single factor...
0
 
LVL 84

Expert Comment

by:ozo
ID: 22717279
Could you give an example of what you mean?
0
 

Author Comment

by:steenpat
ID: 22720552
say i have a box,
it has dimensions 11'6 x 10'1,
the new dimensions of the box are 12'x10' after rounding the segments
and now area is 120, as opposed to 115.95. I tried your formula with success if there is a constant scale factor, but if the scale changes after each pass through the vertexes, the area is different depending on which vertex it starts with if the polygon has more than four verts(for some reason it calcs right for equal or less than that). the way it calcs polygon area is by using a mathematical formula:
 1/2 * sum of [(x1y2-x2y1) + (x2y3-y2x3)...etc]
0
 

Author Comment

by:steenpat
ID: 22721290
probably some logic issue.. ill do some debugigng and get back to you
0
 
LVL 84

Expert Comment

by:ozo
ID: 22723290
1/2 * sum of [(x1y2-x2y1) + (x2y3-y2x3)...etc]
should work regardless of the scale factor and how many vertices and  where it starts (as long as it ends in the same place)
but I'm not sure what you mean that the scale changes after each pass through the vertexes
0
 
LVL 18

Accepted Solution

by:
JoseParrot earned 500 total points
ID: 22723465
This is not scaling.
The scaling tranformation is a resizing of a geometric entity by multiplying its coordiantes by a scaling factor. In multi dimensional words we can have a different factor for each dimension.
What you are looking for is just rounding, which can be solved by using trunc / round / floor / ceil functions. In your case, round(value): round(10.2) = 10;    round(10.6) = 11;
In C/C++ can be done by casting the variables.
Example: sum( (int)x1*(int)y2 - (int)x2*(int)y1) + (int)x2*(int)y3 - (int)y2*(int)x3 + ...)
Jose
0
 

Author Comment

by:steenpat
ID: 22726721
this is my code.. it is not working.
it cals the mag between vertexes and rounds that number then readjusts each vertex in the struct, to start from 0,0 on forwards.
void RoundPolygonVertices( VERTEXES& vcVertexes, double dRound )
	{
		VERTEXES vt;
		CVertex v0,v1;
		long lCount = vcVertexes.size();
 
		for( int i = 0; i < lCount; ++i )
		{
			if(i)
			{
				double dLength = Magnitude(vcVertexes[i], vcVertexes[i-1]);
				double slope=(vcVertexes[i].dY-vcVertexes[i-1].dY)/(vcVertexes[i].dX-vcVertexes[i-1].dX);
				double slopeAngle;
				if(!_isnan(slope))
					slopeAngle=atan(slope);
				else
				{
					slope=0;
					slopeAngle=0;
				}
 
				if( dLength > 0.0 && dRound > 0.0 )
				{
 
					if(fmod( dLength, dRound) > 0)
					{
						double dUpper, dLower;
						dLower = floor(dLength / dRound) * dRound;
						dUpper = dLower+dRound;
 
						if((dLength-dLower) >= (dUpper-dLength))
							dLength=dUpper;
						else
							dLength=dLower;
					}
				
					v1=vcVertexes[i]-vcVertexes[i-1];
 
					if(v1.dX)
						v1.dX=v1.dX/fabs(v1.dX);
					if(v1.dY)
						v1.dY=v1.dY/fabs(v1.dY);
 
					v1.dY=v1.dY*fabs(sin(slopeAngle)*dLength);
					v1.dX=v1.dX*fabs(cos(slopeAngle)*dLength);
 
					v0=v1+v0;
				}
 
				vt.push_back(v0);
					
			}
			else
				vt.push_back( v0 );
				
		}
 
		vcVertexes.clear();
		vcVertexes.insert( vcVertexes.begin(), vt.begin(), vt.end() );

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Dice Roll Probabilities 3 130
STDEVP in SQL 2 91
What is sub-make ? 2 88
DirectoryWatch - Any Algorithm to get workable result with ReadDirectoryChangesW() 3 67
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question