Solved

how to create continuous lines in GDI+ using interpolation?

Posted on 2008-10-01
4
978 Views
Last Modified: 2012-05-05
Hi, I have this problem when i make a line of rectangles only knowing the beginning and the end coords, its all ok, except when the line is almost in vertical position the line becomes dotted i know this is because the Vx is become near to X but, is there a function or method in GDI+  where i can trace a line and to know the coords of an arbitrary point along the line? or another formula for to use for describing a line?   to know  the coords of the points along the line is really necessary.

Thank you.


e.SmoothingMode = SmoothingMode.HighQuality;
                float Cx = this.posx;
                float Cy = this.posy;
                float Fx = this.finalx;
                float Fy = this.finaly;
 
                float Vx = Cx;
           
                float Inc;
                Inc=1;
 
                float a = (Cy - Fy) / (Cx - Fx);
                float b = (Cx * Fy - Fx * Cy) / (Cx - Fx);
 
                   if (Cx > Fx) Inc = -1;
 
                   while(Vx != Fx)
                {
                               ///////by Interpolation  y=ax+b
                                ///////where a=(y1-y2)/(x1-x2)
                               //////b =(x1*y2-x2*y1)/(x1-x2)
 
 
                    Vy = a * Vx + b;
                   e.DrawRectangle(Pens.Red, Vx, Vy, 4, 4);
                    Vx = Vx + Inc;
                }

Open in new window

0
Comment
Question by:eyebirdgto
[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
  • 2
4 Comments
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 22620280
you could set quality to:
myGraphics.SmoothingMode = SmoothingMode.AntiAlias;

and draw the rectangle with lines, instead of placing a dot for each different pixel
myGraphics.DrawLine(myPen, 0, 0, 12, 8);
this should improve drawing quality
0
 

Author Comment

by:eyebirdgto
ID: 22620455
Thank you but, I  know that, the question is I need to know the coords of any point along the line given only the beginning coord (origin of line) and the end with the formula
y=ax+b  

the line is ok except near of 0 and 180 degress at those positions the dots become too spare.

this is for to make a cnc (control numerical machine attached to the serial port of the PC ) trace every position in the line, I read every position x,y and send by the serial port the coords x,y of every pixel along the line, positioning the tool in every point x,y in a work table. (if i put a circle the machine trace a circle in the worktable, if a line then the machine trace a line).
0
 
LVL 18

Accepted Solution

by:
JoseParrot earned 250 total points
ID: 22625057
Hi,
The intuitive algorithm to plot a line is single loop as in ALGO 1 below. In the special case of starting point P1 and P2 are in the same vertical, say x1 = x2, then the lineplot function will draw just one pixel!
In this same algorithm, if difference between x1 and x2is small, it can happens, as you noticed, to draw few points in the interval P1 to P2. This is because you can have several y values for a given x value.
If we verify which is bigger, the x2-x1 difference or y2-y1 difference, we can decide to make the loop by incrementig (or decrementing) x values from x1 to x2, case contraire, from y1 to y2, as in ALGO 2.
To decide wich loop to do (loop by x or by y) the comparaison must be made with the absolute value of the difference, what matters is how many pixels will have the line.
Please note, if x1=x2, then the division by zero error occurs, then test if it is zero, if so, do a simple loop by looping from y1 to y2 with constant x1 value.
The implementation can be something like PSEUDOCODE 1 below.
Jose
 

ALGO 1
lineplot(x1,y1,x2,y2)
{
   for x=x1 to x2
   {
      y = f(x) 
      setPixel(x,y)
   }
}
 
ALGO 2
lineplot(x1,y1,x2,y2)
{
   // verify which is bigger: |x2-x1| or |y2-y1|
   if |x2-x1| > |y2-y1|
   then loop x values from x1 to x2
   else loop y values from y1 to y2
}
 
PSEUDOCODE 1
lineplot(x1,y1,x2,y2)
{
   if abs(x2-x1) > abs(y2-y1) 
   then // loop by varying x values
   {
      dx = abs(x2-x1) / (x2-x1) // will be +1 or -1
      // calculate line equation
      m = (y2-y1) / (x2-x1) // if x2=x1 then the line is vertical
      c = y1 - m*x1
      for x=x1 to x2 step dx
      {
         y = m*x + c
         setPixel(x,y)
      }
   }
   else
   {
      // do the equivalent by varying y values
   }
}
      

Open in new window

0
 

Author Closing Comment

by:eyebirdgto
ID: 31502082
thank you, i have already thought about that, but i had implemented in a bad way its better your solution, thank you.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Okay. So what exactly is the problem here? How often have we come across situations where we need to know if two strings are 'similar' but not necessarily the same? I have, plenty of times. Until recently, I thought any functionality like that wo…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

697 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