|
How about this.
void AddMidpoints(List<Point> points) {
for (int i = points.Count - 1; i >= 1; i -= 1) {
points.Insert(i, midpoint(points[i], points[i - 1]));
}
}
Point midpoint(Point p1, Point p2) {
return new Point(((p1.X + p2.X) / 2), ((p1.Y + p2.Y) / 2));
}
|
|
|
|
|
I understood the question as taking a line created by a number of points and creating a new set of points that construct the same line, but with the same number of points evenly distributed across the line. If that is the case, here is a solution. Theoretically, all you need to do is calculate the length of the line and divide it into even segments according to the number of points provided by the user. Where P1 and P2 are the end points of the line, and n is the number of points provided by the user; you can calculate an x offset as (| P1.X - P2.X |) / n and a y offset as (| P1.Y - P2.Y |) / n . You can then construct a collection of points containing the first and last point provided by the user along with an additional n - 2 points (indices 1...n-1) where the X coordinate = P1.X + (index * x offset) and the Y coordinate = P1.Y + (index * y offset) .
Pseudocode:
N = userPoints.Count
P1 = userPoints[0]
P2 = userPoints[N - 1]
xOffset = Abs(P1.X - P2.X) / N
yOffset = Abs(P1.Y - P2.Y) / N
List newPoints = new List(N)
newPoints.Add(P1)
For I = 1; I < N - 1; I++
newPoints.Add(new Point(P1.X + I * xOffset, P1.Y + I * yOffset))
newPoints.Add(P2)
Keep It Simple Stupid! (KISS)
|
|
|
|
|
Hi,
I admit your approach is very KISS, however I doubt this is the right answer. You are assuming it is a straight line, but then why would the OP start of with a collection of points to define the line? It looks more like: here is a number of points, imagine a fluent line that goes through all of them and come up with new point that lie on that fluent line but now are equally spaced. So you might try some Bezier curves and calculate lengths on those (something I haven't encountered before).
|
|
|
|
|
Hello all and Happy new Year
I have a UserControl which contains a Label and a TextBox and I want to set the Label's Text in my main form. I know I can get a string as parameter in UC's constructor or define the Label, publicly. But I wonder if I can set the Text in Design mode. I mean is there any way to add a property for my UC so when I open Properties tab I can see that and change its value?
I hope I could explain well.
Thanks a lot
I died as a mineral and became a plant,
I died as plant and rose to animal,
I died as animal and I was Man.
Why should I fear? When was I less by dying?
-- Rumi[^]
My blog
|
|
|
|
|
|
Actually I didn't know from where I should start. I tried constructor but I have error adding UC in my windows form. I defined the Label publicly but I could only access the Label this way: UserControl.Label . I searched MSDN for Property and Attribute but I couldn't find anything yet.
Thanks for reply
I died as a mineral and became a plant,
I died as plant and rose to animal,
I died as animal and I was Man.
Why should I fear? When was I less by dying?
-- Rumi[^]
My blog
|
|
|
|
|
You can override the Text property of the usercontrol so as to set the entered text into the label.
Something like what is given below.
[Browsable(true)]
public override string Text
{
set
{
label1.Text = value;
}
}
This will let you set the text in design time.
|
|
|
|
|
Yes, that's the answer: [Browsable(true)]
You made my day, thanks a lot
I died as a mineral and became a plant,
I died as plant and rose to animal,
I died as animal and I was Man.
Why should I fear? When was I less by dying?
-- Rumi[^]
My blog
|
|
|
|
|
I created two instances of my UC in my Form and when I change the Text value of one of them, both will change! I wrote code below:
[Browsable(true)]
public string LabelText
{
get { return label1.Text; }
set { label1.Text = value; }
}
I couldn't understand what is the problem, do you have any idea?
I died as a mineral and became a plant,
I died as plant and rose to animal,
I died as animal and I was Man.
Why should I fear? When was I less by dying?
-- Rumi[^]
My blog
|
|
|
|
|
I just tried using the same code that u gave and it seems to work fine for me. Can you give me the code for the control and also the code for ur form. It might help.
|
|
|
|
|
How is the label1 defined in your UC, is it static or any other reasons why it could be shared along the instances?
|
|
|
|
|
Solved Mika and Member 4470354,
I think I was changed PropertyBinding, DatBinding or I don't know, something similar...
I reset the values.
Thank you both.
I died as a mineral and became a plant,
I died as plant and rose to animal,
I died as animal and I was Man.
Why should I fear? When was I less by dying?
-- Rumi[^]
My blog
|
|
|
|
|
Hello people:
I have been trying hard to find a way to customize the to part in th email address, with no avail. Can you be of help?
I am creating a refer the link webpart where i get the name and email for the TO and the name and email for the FROM. I can achieve the basic functionality using the SendMail but the I want the From part in the email to appear like this -
From: name(sender@senderDomaain.com)
So if Sam sent an email to Samantha and Sam's email address was sam@gmail.com. The from should look like this -
From: Sam(sam@gmail.com)
To: samantha@gmail.com
Any idea how i can append the name to the from email id ?
- Cheers
|
|
|
|
|
Hi guys,
I'm writing a SQLite wrapper but I'm having trouble handling pointers.
Example:
Original C function (from SQLite doc):
const char *sqlite3_column_name(sqlite3_stmt*, int N);
Import in C#:
[DllImport("sqlite3.dll", EntryPoint = "sqlite3_column_name")]
private static extern string sqlite3_column_name(IntPtr sqlite3_stmt, int N);
This works, but sometimes (I dont understand why yet) it gives me back an Access Violation Exception.
The same happens with this:
const char *sqlite3_libversion(void);
[DllImport("sqlite3.dll", EntryPoint = "sqlite3_libversion")]
private static extern string sqlite3_libversion();
That always raise an Access Violation Excpetion.
What I'm doing wrong?
Thank everyone in advance.
-MR
|
|
|
|
|
|
Yep, but since I'm quite new to C# I would like to make one myself, for practicing in making wrappers for DLLs. My question is more about pointers to Char and strings than about SQLite.
Btw Thank you, I'll check that asap
|
|
|
|
|
In that case you can download source code of sqlite wrapper and research the code.
|
|
|
|
|
Hi,
Maybe your problem is connected with string marshalling. Native functions returns char* strings - 1 byte per letter, as far as I know, c# automatically tries to marshal strings as unicode charaters (2B per letter), try maybe such code:
[DllImport("sqlite3.dll", EntryPoint = "sqlite3_libversion", CharSet=CharSet.Ansi)]<br />
private static extern string sqlite3_libversion();
Alternatively you could also try using this attribute:
[return:MarshalAs(UnmanagedType.LPStr)]
|
|
|
|
|
Thank you for your reply,
unfortunately I had no luck with your tips. Googling I found a solution, even if it seems a workaround to me instead a properly solution.
[DllImport("kernel32")]
private extern static int lstrlen(IntPtr str);
String PointerToString(IntPtr pointer)
{
Encoding enc = Encoding.UTF8;
int pLen = 0;
if (pointer == IntPtr.Zero)
pLen = 0;
else
pLen = lstrlen(ptr);
Byte[] bytes = new Byte[pLen];
Marshal.Copy(pointer, bytes, 0, pLen);
return enc.GetString(bytes, 0, pLen);
}
|
|
|
|
|
Hi,
I've checked my solution on functions that return ascii strings and unicode strings, and those attributes work pretty well, so maybe the problem is somewhere else?
Btw in function you are using, you have utf8 encoding, but you assume that strings are 1B per letter, which is not always true for utf8.
|
|
|
|
|
I tryed to sort out the problem so I made this
public partial class Form1 : Form
{
[DllImport("sqlite3.dll", EntryPoint = "sqlite3_libversion",CharSet=CharSet.Ansi)]
private static extern string sqlite3_libversion();
[return: MarshalAs(UnmanagedType.LPStr)]
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
sqlite3_libversion();
}
}
Kept it simplier as possible.
This should return a string with the version of sqlite. But this always gives the Accession Violation Exception. You can easily try this downloading the sqlite3.dll from the official site.
Thank for you help I really appreciate.
|
|
|
|
|
Hi,
This code should work flawlessly, I've tried it on the newest library. Program prints "3.6.7"
public class Program
{
[DllImport("sqlite3.dll")]
[return: MarshalAs(UnmanagedType.LPStr)]
private static extern string sqlite3_libversion();
public unsafe static void Main()
{
Console.WriteLine(sqlite3_libversion());
}
}
Few hints:
1. If you want to bind attribute to function, you have to place function under [] tags.
2. EntryPoint has to be given if c# function name differs from function declared in native dll
|
|
|
|
|
Thank you so much for your patience
|
|
|
|
|
|
Hi,
In Windows Vista, while calling a process from a service......I am using CreateProcessAsUser()
( If better way possible, please suggest )
As you know, while using CreateProcessAsUser(), we need a token. I am obtaining this using DuplicateTokenEx() along with other functions.
It's syntax is : bool DuplicateTokenEx(.......,ref SECURITY_ATTRIBUTES lpTokenAttributes, SECURITY_IMPERSONATION_LEVEL ImpersonationLevel, TOKEN_TYPE TokenType,........);
When I declare this, I get an error saying,
"The type or namespace name 'SECURITY_ATTRIBUTES' could not be found (are you missing a using directive or an assembly reference?)"
Same applies to SECURITY_IMPERSONATION_LEVEL and TOKEN_TYPE.
Questions :
1. Can anybody please let me know the probable reason for this and also the solution ?
2. Which directive/assembly is this referring to?
Awaiting your reply......
Thank You
|
|
|
|