geometry

Shortest distance between two line segments

I need a function to find the shortest distance between two line segments. A line segment is defined by two endpoints. So for example one of my line segments (AB) would be defined by the two points A (x1,y1) and B (x2,y2) and the other (CD) would be defined by the two points C (x1,y1) and D (x2,y2). Feel free to write the solution in an...

Generate a polygon from line.

I want to draw a line with thickness in j2me. This can easily be achieved in desktop java by setting Pen width as thickness value. However in j2me, Pen class does not support width. My idea is to generate a polygon from the line I have, that resembles the line with thickness i want to draw. In picture, on the left is what I have, a line ...

Draw a parallel line

I have x1,y1 and x2,y2 which forms a line segment. How can I get another line x3,y3 - x4,y4 which is parallel to the first line as in the picture. I can simply add n to x1 and x2 to get a parallel line but it is not what i wanted. I want the lines to be as parallel in the picture. ...

Converting from Latitude/Longitude to Cartesian Coordinates with a World File and map image.

I have a java applet that allows users to import a jpeg and world file from the local system. The user can then "click" draw lines on the image that was imported. Each endpoint of each line contains a set of X/Y and Lat/Long values. The XY is standard java coordinate space, the applet uses an affine transform calculation with the worl...

Project Euler #163 understanding

I spent quite a long time searching for a solution to this problem. I drew tons of cross-hatched triangles, counted the triangles in simple cases, and searched for some sort of pattern. Unfortunately, I hit the wall. I'm pretty sure my programming/math skills did not meet the prereq for this problem. So I found a solution online in ord...

Find the centroid of a polygon with weighted vertices

Hi, I know how to find the centroid (center of mass) of a regular polygon. This assumes that every part of the polygon weighs the same. But how do I calculate the centroid of a weightless polygon (made from aerogel perhaps :), where each vertex has a weight? Simplified illustration of what I mean using straight line: 5kg-------------...

Algorithm: Determine shape of two sectors delineated by an arbitrary path, and then fill one.

NOTE: This is a challenging problem for anybody who likes logic problems, etc. Consider a rectangular two-dimensional grid of height H and width W. Every space on the grid has a value, either 0 1 or 2. Initially, every space on the grid is a 0, except for the spaces along each of the four edges, which are initially a 2. Then consider a...

Axis Rotation Question

By using left hand rule, I rotate one object left and right using y axis, and rotate up/down using x axis. After first object is rotated to the right, the up/down rotation should be using z axis. However, when I try to rotate using z axis, after the first rotation, it has the same effect when I rotate using y axis. Anyone has any ideas...

Cone-Line Segment Intersection 2D

I would like to know if is there any way to determine if a cone is intersecting with a (finite) line segment. The cone is actually a circle located at P(x,y) with theta degree field of view and radius r: I'm trying to do it in C# but I don't have any idea how to that, so for now this is what I'm doing: Check if the line segment is i...

Graphics Question: How do I restrict the mouse cursor to within a circle?

I'm playing with XNA. When I click the left mouse button, I record the X,Y co-ordinates. Keeping the mouse button held down, moving the mouse draws a line from this origin to the current mouse position. I've offset this into the middle of the window. Now, what I'd like to do is restrict the mouse cursor to within a circle (with a radius...

An implementation of Sharir's or Aurenhammer's deterministic algorithm for calculating the intersection/union of 'N' circles

The problem of finding the intersection/union of 'N' discs/circles on a flat plane was first proposed by M. I. Shamos in his 1978 thesis: Shamos, M. I. “Computational Geometry” Ph.D. thesis, Yale Univ., New Haven, CT 1978. Since then, in 1985, Micha Sharir presented an O(n log2n) time and O(n) space deterministic algorithm for the disc...

Sort latitude and longitude coordinates into clockwise ordered quadrilateral

Problem Users can provide up to four latitude and longitude coordinates, in any order. They do so with Google Maps. Using Google's Polygon API (v3), the coordinates they select should highlight the selected area between the four coordinates. Question How do you sort an array of latitude and longitude coordinates in (counter-)clockwise...

Make "page-turning" transform (3D) follow x-axis

Hello I'm making an eBook reader for the iPhone. I have made a page-turn with CABasicAnimation and it's working great. Now I want to do the page-turn "manually" by using the x-coordinate when your finger moves along the screen. I've achieve that, but it needs some tweaking to feel more "real". Because of the 3D effect (defined by transf...

Find right- and leftmost 2d point of a horizontal circle in 3d Vector environment

I'm drawing a 3D pie chart that is calculated with in 3D vectors, projected to 2D vectors and then drawn on a Graphics object. I want to calculate the most left and right point of the circle after projected in 2d. (So not 0 and 1 Pi!) The method to create a vector, draw and project to a 2d vector are below. Anyone knows the answer? publ...

Finding smallest angle between vectors in logarithmic time.

I have n=10000 10-dimensional vectors. For every vector v1 I want to know the vector v2 that minimizes the angle between v1 and v2. Is there a way to solve this problem faster than O(n^2)? ...

Find the point with radius and angle

Hi, I'm not a genius in geometry, I'd like to find a point in as3 with the radius and a angle but I don't remember the rule, I know this should be simple! Here's an example: ...

Drawing an honeycomb with as3

Hi, I'm trying to create an honeycomb with as3 but I have some problem on cells positioning. I've already created the cells (not with code) and for cycled them to a funcion and send to it the parameters which what I thought was need (the honeycomb cell is allready on a sprite container in the center of the stage). to see the structure ...

Transformed vertex after reflection over the x-axis

A figure has a vertex (x, -y). What are the coordinates of the transformed vertes after a reflection over the x-axis? ...

Choosing circle radius to fully fill a rectangle

Hi, the pixman image library can draw radial color gradients between two circles. I'd like the radial gradient to fill a rectangular area defined by "width" and "height" completely. Now my question, how should I choose the radius of the outer circle? My current parameters are the following: A) inner circle (start of gradient) center p...

comparing two angles

Given four points in the plane, A,B,X,Y, I wish to determine which of the following two angles is smaller ∢ABX or ∢ABY. The angle ∢ABX is defined as the angle of BX, when AB is translated to lie on the open segment (-∞,0]. Intuitively when saying ∢ABX I mean the angle you get when you turn left after visiting vertex B. I'd rather not ...