Results 1 to 3 of 3

Thread: Draw line on a form

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Location
    Auckland, NZ
    Posts
    182
    Hi guys,

    Please explain me how can I find whether any drawn line on the form crossed or not Image box. Line has started coordinates X, Y when mouse button pressed and final coordinates X, Y when it was released. And Image – say small circle with coordinates X, Y and also width ~ 15 pixels. I appreciate any help.

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    You want to check if a line intersects a rectangle is it ?
    Well in java I had to do something similar.
    This code might help (its easily ported) ;

    Code:
    	private static boolean isCross(myVertex lne1, myVertex lne2) {
    		boolean toReturn = false;    
    		double dx1 = lne1.x2 - lne1.x1;
    		double dy1 = lne1.y2 - lne1.y1;
    		double dx2 = lne2.x2 - lne2.x1;
    		double dy2 = lne2.y2 - lne2.y1;
    		double n = 0.0;
    		double s = 0.0;
    		
    		if ((dx2 * dy1) - (dy2 * dx1) != 0) {
    			n = (dx1 * (lne2.y1 - lne1.y1) + dy1 * (lne1.x1 - lne2.x1)) / (dx2 * dy1 - dy2 * dx1);
    			s = (dx2 * (lne1.y1 - lne2.y1) + dy2 * (lne2.x1 - lne1.x1)) / (dy2 * dx1 - dx2 * dy1);
    			toReturn = ((s >= 0) && (s <= 1) && (n >= 0) && (n <= 1));
    		}
    
    		return toReturn;		
    	}
    
    class myVertex {
    	int x1;
    	int y1;
    	int x2;
    	int y2;
    }
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Location
    Auckland, NZ
    Posts
    182
    Thanks a lot, plenderj. It just what I needed. I have tried it tonight and it worked fine.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width