Jump to content

Having a serious problem in doing a simple math calculation...


Xhin
 Share

Recommended Posts

Basically, I wrote a cool plugin a few days ago, but in the testing process, I set it up so it only worked on 800x600 canvases. There's a value called Amount1 that will pick a section of the X canvas, so it goes from 0 to 800.

Obviously this doesn't work in all cases, so what I've been trying to do is make Amount1 instead be a percentage which is calculated into a pixel amount. So for example, if Amount1 = 50; and the canvas size is 800x600, then Amount1 will be converted to 400. If Amount1 = 50; and the canvas size is 233x600, then Amount1 should round down to 116.

This should be very simple.

Instead, I keep running into conversion problems. If I get those right somehow (by affixing an (int) or (double) or whatever), then the results aren't what they're supposed to be, I get absurdly large numbers, and the entire program fails.

For example, Amount1 = 60; CenterX = 400; (Canvas size is 800x600). I run:

Amount1 = (Amount1*CenterX*2)/100; 

This should be 480. In fact, running

if (Amount1 == 480) { <insert stuff I use to debug with here> }

proves that it's 480. But for some reason setting Amount1 by the code above and setting it by a simple "Amount1 = 480;" are completely different. It's not a type problem, since the same thing happens if I do:

Amount1 = (int)((Amount1*CenterX*2)/100); 

it also happens if I do

Amount1 *= (CenterX*2); Amount1 /= 100; 

or if I set Amount1 equal to 1/100th of itself (as a double) and simply multiply it by CenterX*2 and reconvert to (int).

What's weird is the absurdly large number I get when the system throws an error message: "System.ArgumentOutOfRangeException: Coordinates out of range, max={Width:799, Height=599{Parameter name: (x,y)Actual value was {X=-6039228,Y=19}."

What am I missing here? I've been trying to fix this for hours without success.

xhin.png

Link to comment
Share on other sites

Useful for calculating the centre of an image/selection:

// Rectangle selection = this.EnvironmentParameters.GetSelection(src.Bounds).GetBoundsInt();

// long CenterX = (long)(((selection.Right - selection.Left) / 2)+selection.Left);

// long CenterY = (long)(((selection.Bottom - selection.Top) / 2)+selection.Top);

(Find these code samples and MANY more at www.BoltBait.com)

Now you can multiply the results by your % slider value.

Link to comment
Share on other sites

Hi Xhin,

It looks like you're forgetting to add in the 'selection.Left ' bit. (perhaps that's overly simplistic).

Looks like you are assuming that centreX = 400 on an 800 wide canvas, it probably isn't. (so you may need to subract selection.left from your numbers before doing any calculation - then add it back in before giving a value to the dst pixel).

It can get confusing! - I sometimes add a little 'if' statement to protect against out of bounds exeptions - so at least the plugin runs and I can see where I've gone wrong.

eg

 if(X >= rect.Left && X < rect.Right){}

I've put my codelab source code for 'squirkle' here : http://forums.getpai...23194-squirkle/

That has some code for positioning things on the canvas, that may be useful to read.

Good luck - perseverance is sometimes the most useful skill.

 

Red ochre Plugin pack.............. Diabolical Drawings ................Real Paintings

 

PdnForumSig2.jpg

Link to comment
Share on other sites

Here's some code I used for the Polaroid Frame plugin. It uses integer math to work out % sizes

// Delete these 2 lines if you don't need to know the center point of the current selection
double px = ( Amount2.First +1 ) /  2 ;
double py = ( Amount2.Second + 1 ) / 2;
int CenterX = (int)Math.Round ( px * ( selection.Right - selection.Left ) ) ;
int CenterY = (int)Math.Round ( py * ( selection.Bottom - selection.Top ) ) ;
// Delete these lines if you don't need the primary or secondary color
ColorBgra PrimaryColor = (ColorBgra)EnvironmentParameters.PrimaryColor;
ColorBgra SecondaryColor = (ColorBgra)EnvironmentParameters.SecondaryColor;
//	ColorBgra FrameColor;
ColorBgra Pix;
// Specific variables
//	int FrmHeight = Amount1 * 1350 / 1000;  // 135% of img height
//	int FrmWidth = Amount1 * 1100 / 1000; // 110% of img height
int FrmTop = CenterY - ( Amount1 * 1350 / 2000);
int FrmBtm = CenterY + ( Amount1 * 1350 / 2000);
int FrmLt = CenterX - ( Amount1 * 1100 / 2000);
int FrmRt = CenterX + ( Amount1 * 1100 / 2000);

//	int TopBorder = Amount1 * 75 / 1000;  // 7.5% of img height

//	int ImgHeight = Amount1;  //  Image Height
//	int ImgWidth = Amount1 * 975 / 1000; // 97.5% of img height
int ImgTop = FrmTop + (Amount1 * 75 / 1000);
int ImgBtm = ImgTop + ( Amount1 / 2 * 2 );	  // CHECK Binary OP!!
int ImgLt = CenterX - ( Amount1 * 975 / 2000 );
int ImgRt = CenterX + ( Amount1 * 975 / 2000 );

note: I'm dividing by 2000 to get half of the stated % (i.e. the distance from the center point)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...