Click here to Skip to main content
15,914,071 members
Home / Discussions / C#
   

C#

 
Questionhow to use cassandra and hadoop in c#.net web application Pin
Mayank Engineer8-Nov-13 19:36
Mayank Engineer8-Nov-13 19:36 
AnswerRe: how to use cassandra and hadoop in c#.net web application Pin
Richard MacCutchan8-Nov-13 23:03
mveRichard MacCutchan8-Nov-13 23:03 
Question"select" statement in C# with a string array Pin
moseslmathew8-Nov-13 18:43
moseslmathew8-Nov-13 18:43 
AnswerRe: "select" statement in C# with a string array Pin
Mycroft Holmes8-Nov-13 20:06
professionalMycroft Holmes8-Nov-13 20:06 
AnswerRe: "select" statement in C# with a string array Pin
OriginalGriff8-Nov-13 23:24
mveOriginalGriff8-Nov-13 23:24 
SuggestionRe: "select" statement in C# with a string array Pin
Richard Deeming11-Nov-13 1:21
mveRichard Deeming11-Nov-13 1:21 
AnswerRe: "select" statement in C# with a string array Pin
Richard Deeming11-Nov-13 1:27
mveRichard Deeming11-Nov-13 1:27 
QuestionGraphic Program Help Pin
Gengou8-Nov-13 9:32
professionalGengou8-Nov-13 9:32 
Hey,
I need some help with a program. The task is: I have 2n points in a list. I want to pair every points in the list by drawing n lines, in such a way that the sum of the lines distances is minimum. After some searches, I tried an implententation with the closest-pair algorithm, but that not really fulfill's the task. The sum can be smaller because of my last pair of "closest-points". So now I kinda have no idea how proceed with this, or what other idea should I consider to implement at all. Here is my implementation (which doesn't fulfill's the task):


C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace Apropiere_Puncte
{
    public partial class Form1 : Form
    {
        List<PointF> points = new List<PointF>();
 
        public Form1()
        {
            InitializeComponent();
 
            Random r = new Random();
 
            for (int i = 0; i < 20; i++)
            {
                PointF p = new PointF(r.Next() % this.Size.Width - 20, r.Next() % this.Size.Height - 20);
                if (p.X < 20)
                    p.X = 20;
                if (p.Y < 20)
                    p.Y = 20;
                points.Add(p);
            }
        }
 
        public class Segment
        {
            public Segment(PointF p1, PointF p2)
            {
                P1 = p1;
                P2 = p2;
            }
 
            public readonly PointF P1;
            public readonly PointF P2;
 
            public float Length()
            {
                return (float)Math.Sqrt(LengthSquared());
            }
 
            public float LengthSquared()
            {
                return (P1.X - P2.X) * (P1.X - P2.X) + (P1.Y - P2.Y) * (P1.Y - P2.Y);
            }
        }
 
        public static Segment Closest_BruteForce(List<PointF> points)
        {
            int n = points.Count;
            var result = Enumerable.Range(0, n - 1)
                .SelectMany(i => Enumerable.Range(i + 1, n - (i + 1))
                    .Select(j => new Segment(points[i], points[j])))
                    .OrderBy(seg => seg.LengthSquared())
                    .First();
 
            return result;
        }
 
        public static Segment MyClosestDivide(List<PointF> points)
        {
            return MyClosestRec(points.OrderBy(p => p.X).ToList());
        }
 
        private static Segment MyClosestRec(List<PointF> pointsByX)
        {
            int count = pointsByX.Count;
            if (count <= 4)
                return Closest_BruteForce(pointsByX);
 
            // left and right lists sorted by X, as order retained from full list
            var leftByX = pointsByX.Take(count / 2).ToList();
            var leftResult = MyClosestRec(leftByX);
 
            var rightByX = pointsByX.Skip(count / 2).ToList();
            var rightResult = MyClosestRec(rightByX);
 
            var result = rightResult.Length() < leftResult.Length() ? rightResult : leftResult;
 
            // There may be a shorter distance that crosses the divider
            // Thus, extract all the points within result.Length either side
            var midX = leftByX.Last().X;
            var bandWidth = result.Length();
            var inBandByX = pointsByX.Where(p => Math.Abs(midX - p.X) <= bandWidth);
 
            // Sort by Y, so we can efficiently check for closer pairs
            var inBandByY = inBandByX.OrderBy(p => p.Y).ToArray();
 
            int iLast = inBandByY.Length - 1;
            for (int i = 0; i < iLast; i++)
            {
                var pLower = inBandByY[i];
 
                for (int j = i + 1; j <= iLast; j++)
                {
                    var pUpper = inBandByY[j];
 
                    // Comparing each point to successivly increasing Y values
                    // Thus, can terminate as soon as deltaY is greater than best result
                    if ((pUpper.Y - pLower.Y) >= result.Length())
                        break;
 
                    Segment segment = new Segment(pLower, pUpper);
                    if (segment.Length() < result.Length())
                        result = segment;// new Segment(pLower, pUpper);
                }
            }
            return result;
        }
 
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            foreach (PointF p in points)
            {
                e.Graphics.DrawEllipse(new Pen(Color.Black), p.X - 1, p.Y - 1, 2, 2);
            }
 
            while (points.Count >= 2)
            {
                var result2 = MyClosestDivide(points);
                e.Graphics.DrawLine(new Pen(Color.Green), result2.P1, result2.P2);
                points.Remove(result2.P1);
                points.Remove(result2.P2);
            }
        }
    }
}


Sorry for my english and thanks for help,
Best regards
AnswerRe: Graphic Program Help Pin
BillWoodruff9-Nov-13 1:43
professionalBillWoodruff9-Nov-13 1:43 
QuestionVisio and C# Pin
Raju_N8-Nov-13 9:13
professionalRaju_N8-Nov-13 9:13 
Questionc# coding a progress bar with barkground worker Pin
PClarkeirl8-Nov-13 3:35
PClarkeirl8-Nov-13 3:35 
AnswerRe: c# coding a progress bar with barkground worker Pin
Richard Andrew x648-Nov-13 3:45
professionalRichard Andrew x648-Nov-13 3:45 
AnswerRe: c# coding a progress bar with barkground worker Pin
BillWoodruff8-Nov-13 3:55
professionalBillWoodruff8-Nov-13 3:55 
AnswerRe: c# coding a progress bar with barkground worker Pin
OriginalGriff8-Nov-13 5:14
mveOriginalGriff8-Nov-13 5:14 
Questionhow to store radiobutton status in app.config Pin
Member 102635197-Nov-13 23:11
Member 102635197-Nov-13 23:11 
AnswerRe: how to store radiobutton status in app.config Pin
Marco Bertschi8-Nov-13 0:19
protectorMarco Bertschi8-Nov-13 0:19 
GeneralMessage Closed Pin
8-Nov-13 1:06
Member 102635198-Nov-13 1:06 
GeneralRe: how to store radiobutton status in app.config Pin
Nicholas Marty8-Nov-13 3:02
professionalNicholas Marty8-Nov-13 3:02 
GeneralRe: how to store radiobutton status in app.config Pin
Member 1026351910-Nov-13 19:45
Member 1026351910-Nov-13 19:45 
QuestionGetting the value of TreeList in DevExpress Pin
nhanlaptrinh7-Nov-13 23:02
nhanlaptrinh7-Nov-13 23:02 
AnswerRe: Getting the value of TreeList in DevExpress Pin
Eddy Vluggen8-Nov-13 5:01
professionalEddy Vluggen8-Nov-13 5:01 
Questionforeach (TerminalGroup tg in vps) return tg; Pin
Marc W7-Nov-13 22:52
Marc W7-Nov-13 22:52 
AnswerRe: foreach (TerminalGroup tg in vps) return tg; Pin
Pete O'Hanlon7-Nov-13 23:47
mvePete O'Hanlon7-Nov-13 23:47 
GeneralRe: foreach (TerminalGroup tg in vps) return tg; Pin
harold aptroot7-Nov-13 23:54
harold aptroot7-Nov-13 23:54 
GeneralRe: foreach (TerminalGroup tg in vps) return tg; Pin
Marc W8-Nov-13 0:11
Marc W8-Nov-13 0:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.