15,791,739 members
1.00/5 (1 vote)
See more:
```This is a kind of graphical test in which the subject has to find two lines using any four points in the rectangle to fit three geometric figures in the main figure. To make sure the subject hit exactly the dots, I built a circular surface around the points figured by distance 1,2,3 and 4. The goal is click with mouse on the points, and if four points are the good ones, two lines will be built between the pairs of points. If not, nothing. <pre>There seems to be a problem between checking distances d1, d2 / d3, d4 and MouseDown event. There are no errors, but the result is wrong, in the sense that I use "if ((distance1 <= 20 * 20) && (distance2 <= 20 * 20))" the line is not build when I press Drawing button, unless I use a single distance, distance1 or distance2, by clicking the mouse on any of the points 1 and 2. I don't see how I could add an image to be more explicit. <pre>Why 20 * 20? Square root of Distance1, for example, is a mathematical formula that describes the area of a circle with a radius of 20 pixels and its center defined by predefined coordinates that allows the user who mouse clicks on a point to be validated that he clicked correctly if he touches this area.
If you click the mouse exactly on point 1 defined by distance1, this means that distance1 <= 20 * 20, as well as in point 2, the program builds the first line between poin1 and point2.
Continuing to click with the mouse on point 3 defined by the distance3, as well as in point 4, the program builds the second line.
The code does not draw the line when I press point 1 and 2, 3 and 4 consecutively, but identifies correctly all the points because I checked them one by one separately.```

What I have tried:

```using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsAppFG
{
public partial class FormProbaFGpag1 : Form
{
Timer t = new Timer();
static public DateTime startTimeForm = default(DateTime);

public int TickCounter = 0;

public Point StartPoint, EndPoint;

static public Int32 RezP1 = 0;  //daca pag1 este corect facuta
static public double ScorP1 = 0; //scor obtinut, 0.5 sau 1
static public Int32 Pag1Omise = 0;

public Int32 posX = 0;  //coord citite mouse
public Int32 posY = 0;

public Point P1 { get; set; }
public Point P2 { get; set; }
public Point P3 { get; set; }
public Point P4 { get; set; }

public Point FormCoordinates { get; set; }

public Int32 distance1 = 0;
public Int32 distance2 = 0;
public Int32 distance3 = 0;
public Int32 distance4 = 0;

public FormProbaFGpag1()
{
InitializeComponent();

Graphics g = this.CreateGraphics();

this.Cursor = new Cursor(Cursor.Current.Handle);
this.MouseDown += new MouseEventHandler(this.FormProbaFGpag1_MouseDown);
}

private void FormProbaFGpag1_Load(object sender, EventArgs e)
{
//backcolor
this.BackColor = Color.White;

t.Tick += delegate
{
t.Stop();
MessageBox.Show("Timpul a axpirat. Apăsați OK pentru a merge mai departe.");

FormProbaFGpag2 probaFGpag2 = new FormProbaFGpag2();
probaFGpag2.Show();
this.Close();

};
t.Interval = (int)TimeSpan.FromMinutes(0.5).TotalMilliseconds;
t.Start();

}

protected override void OnPaint(PaintEventArgs e)  //creez o functie pe care o pot incarca direct din form load
{
base.OnPaint(e);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);

e.Graphics.Clear(Color.White);
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

Pen p = new Pen(Color.Black, 2);  //2 este grosimea width
Pen pL = new Pen(Color.Blue, 2);  //linie
Pen pR = new Pen(Color.Red, 4);
Pen pAt = new Pen(Color.Transparent, 10);  //punct linie culoare transparent

p.Alignment = System.Drawing.Drawing2D.PenAlignment.Center;
pL.Alignment = System.Drawing.Drawing2D.PenAlignment.Center;
pR.Alignment = System.Drawing.Drawing2D.PenAlignment.Center;
pAt.Alignment = System.Drawing.Drawing2D.PenAlignment.Center;

Rectangle rect1 = new Rectangle();

rect1.X = 100;
rect1.Y = 100;
rect1.Width = 300;
rect1.Height = 300;

e.Graphics.DrawRectangle(p, rect1);

//e.Graphics.DrawEllipse(Pens.Red, x1, x2, wid, wid);

for (Int32 i = 100; i <= 400; i = i + 30)
for (Int32 j = 100; j <= 400; j = j + 30)
{
e.Graphics.DrawEllipse(pR, i, j, 4, 4);
}
//g.DrawEllipse(pR, 150, 100, 4, 4);

p.Dispose();
pL.Dispose();
pR.Dispose();
pAt.Dispose();
e.Graphics.Dispose();
//this.Refresh();

}

private void FormProbaFGpag1_MouseDown(object sender, MouseEventArgs e)
{

FormProbaFGpag1.ActiveForm.PointToScreen(Point.Empty);
FormCoordinates = FormProbaFGpag1.ActiveForm.PointToScreen(e.Location);
posX = e.X;
posY = e.Y;

if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
Point pt = Cursor.Position;
StartPoint = pt;
EndPoint = pt;
ControlPaint.DrawReversibleLine(StartPoint, EndPoint, Color.Black);
}
}

private void buttonDeseneaza_Click(object sender, EventArgs e)
{
double ScorP11 = 0;
double ScorP12 = 0;

Graphics g = this.CreateGraphics();

Pen pAc = new Pen(Color.Red, 2);
Pen pAt = new Pen(Color.Transparent, 10);  //punct linie culoare transparent

P1 = new Point(250, 100);  //1
P2 = new Point(100, 250);  //2
P3 = new Point(400, 250);  //3
P4 = new Point(250, 400);  //4

distance1 = (((posX - 250) * (posX - 250)) + ((posY - 100) * (posY - 100)));
distance2 = (((posX - 100) * (posX - 100)) + ((posY - 250) * (posY - 250)));
distance3 = (((posX - 400) * (posX - 400)) + ((posY - 250) * (posY - 250)));
distance4 = (((posX - 250) * (posX - 250)) + ((posY - 400) * (posY - 400)));

if ((distance1 <= 20 * 20) && (distance2 <= 20 * 20))
{
pAt = pAc;
g.DrawLine(pAt, 250, 100, 100, 250);  // se construiește linia între punctele p1 si p2 de culoare rosie
ScorP11 = 0.5;
}
else
{
ScorP11 = 0;
}

if ((distance3 <= 20 * 20) && (distance4 <= 20 * 20))
{
pAt = pAc;
g.DrawLine(pAt, 400, 250, 250, 400);  // se construiește linia între punctele p1 si p2 de culoare rosie
ScorP12 = 0.5;
}
else
{
ScorP12 = 0;
}

ScorP1 = ScorP11 + ScorP12;

if (ScorP1 == 1) RezP1 = 1;
else if (ScorP1 < 1) RezP1 = 0;

pAc.Dispose();
pAt.Dispose();
g.Dispose();
}

private void FormProbaFGpag1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
ControlPaint.DrawReversibleLine(StartPoint, EndPoint, Color.Black); // erase previous line
EndPoint = Cursor.Position;
ControlPaint.DrawReversibleLine(StartPoint, EndPoint, Color.Black); // draw new line
}
}

private void FormProbaFGpag1_MouseUp(object sender, MouseEventArgs e)
{
/*
Graphics g = this.CreateGraphics();
Pen pAc = new Pen(Color.Red, 2);

if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
ControlPaint.DrawReversibleLine(StartPoint, EndPoint, Color.Black); // erase previous line

// ... do something with StartPont and EndPoint in here ...

// possibly add them to a class level structure to use in the Paint() event to make it persistent?
g.DrawLine(pAc, StartPoint, EndPoint);
}
*/
}

private void buttonPag2NextPage_Click(object sender, EventArgs e)
{
FormProbaFGpag2 probaFGpag2 = new FormProbaFGpag2();
probaFGpag2.Show();
t.Stop();
this.Close();
}
}
}```
Posted
Updated 14-Jun-22 0:46am
v5
OriginalGriff 14-Jun-22 3:22am
And?
What does it do that you didn't expect, or not do that you did?
What have you tried to do to find out why?
Are there any error messages, and if so, where and when? What did you do to make them happen?

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
Member 15293029 14-Jun-22 3:38am
There seems to be a problem between checking distances d1, d2 / d3, d4 and MouseDown event. There are no errors, but the result is wrong, in the sense that I use "if ((distance1 <= 20 * 20) && (distance2 <= 20 * 20))" the line is not build when I press Drawing button, unless I use a single distance, distance1 or distance2, by clicking the mouse on any of the points 1 and 2. I don't see how I could add an image to be more explicit.

## Solution 1

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
```Input   Expected output    Actual output
1            2                 1
2            4                 4
3            6                 9
4            8                16```
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
```private int Double(int value)
{
return value * value;
}```

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!

Member 15293029 14-Jun-22 4:47am
Thanks for the advice and inconvenience, but what you write cannot be called solution. The code is never correct if you don't get the desired result, even without errors, I know, otherwise I would have asked for advice?
OriginalGriff 14-Jun-22 4:49am
So do what I suggested, and use the debugger to find out why.
Unless you know exactly what values you are dealing with, and what exactly happens in your code while it is running, you can't fix it ...